home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 109 / EnigmaAmiga109CD.iso / dalla rivista / host contacted / jikes.lha / jikes-1.11 / src / expr.cpp < prev    next >
C/C++ Source or Header  |  2000-01-06  |  314KB  |  7,232 lines

  1. // $Id: expr.cpp,v 1.50 2000/01/06 08:24:30 lord Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10. #include "double.h"
  11. #include "config.h"
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <sys/stat.h>
  16. #include "parser.h"
  17. #include "semantic.h"
  18. #include "control.h"
  19. #include "table.h"
  20. #include "tuple.h"
  21. #include "spell.h"
  22.  
  23. bool Semantic::IsIntValueRepresentableInType(AstExpression *expr, TypeSymbol *type)
  24. {
  25.     IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  26.  
  27.     return (expr -> IsConstant() &&
  28.             //
  29.             // TODO: the test:
  30.             //
  31.             //    control.IsSimpleIntegerValueType(expr -> type())) &&
  32.             //
  33.             // makes more sense than the test:
  34.             //
  35.             //    expr -> Type() == control.int_type) &&
  36.             //
  37.             // below which is specified in the JLS.
  38.             //
  39.             expr -> Type() == control.int_type) &&
  40.             (type == control.int_type ||
  41.              type == control.no_type  ||
  42.              (type == control.char_type && (literal -> value >= 0)  && (literal -> value <= 65535)) ||
  43.              (type == control.byte_type && (literal -> value >= -128) && (literal -> value <= 127)) ||
  44.              (type == control.short_type && (literal -> value >= -32768)  && (literal -> value <= 32767)));
  45. }
  46.  
  47.  
  48. inline bool Semantic::MoreSpecific(MethodSymbol *source_method, MethodSymbol *target_method)
  49. {
  50.     if (! CanMethodInvocationConvert(target_method -> containing_type, source_method -> containing_type))
  51.         return false;
  52.  
  53.     for (int k = target_method -> NumFormalParameters() - 1; k >= 0; k--)
  54.     {
  55.         if (! CanMethodInvocationConvert(target_method -> FormalParameter(k) -> Type(),
  56.                                          source_method -> FormalParameter(k) -> Type()))
  57.             return false;
  58.     }
  59.  
  60.     return true;
  61. }
  62.  
  63.  
  64. inline bool Semantic::MoreSpecific(MethodSymbol *method, Tuple<MethodSymbol *> &maximally_specific_method)
  65. {
  66.     for (int i = 0; i < maximally_specific_method.Length(); i++)
  67.     {
  68.         if (! MoreSpecific(method, maximally_specific_method[i]))
  69.             return false;
  70.     }
  71.  
  72.     return true;
  73. }
  74.  
  75.  
  76. inline bool Semantic::NoMethodMoreSpecific(Tuple<MethodSymbol *> &maximally_specific_method, MethodSymbol *method)
  77. {
  78.     for (int i = 0; i < maximally_specific_method.Length(); i++)
  79.     {
  80.         if (MoreSpecific(maximally_specific_method[i], method))
  81.             return false;
  82.     }
  83.  
  84.     return true;
  85. }
  86.  
  87.  
  88. void Semantic::ReportMethodNotFound(Ast *ast, wchar_t *name)
  89. {
  90.     SemanticError::SemanticErrorKind kind;
  91.  
  92.     int num_arguments;
  93.     AstExpression **argument;
  94.  
  95.     AstMethodInvocation *method_call;
  96.     if ((method_call = ast -> MethodInvocationCast()))
  97.     {
  98.         kind = SemanticError::METHOD_NOT_FOUND;
  99.         num_arguments = method_call -> NumArguments();
  100.         argument = new AstExpression*[num_arguments + 1];
  101.         for (int i = 0; i < num_arguments; i++)
  102.             argument[i] = method_call -> Argument(i);
  103.     }
  104.     else
  105.     {
  106.         kind = SemanticError::CONSTRUCTOR_NOT_FOUND;
  107.  
  108.         AstClassInstanceCreationExpression *class_creation;
  109.         AstSuperCall *super_call;
  110.  
  111.         if ((class_creation = ast -> ClassInstanceCreationExpressionCast()))
  112.         {
  113.             num_arguments = class_creation -> NumArguments();
  114.             argument = new AstExpression*[num_arguments + 1];
  115.             for (int i = 0; i < num_arguments; i++)
  116.                 argument[i] = class_creation -> Argument(i);
  117.         }
  118.         else if ((super_call = ast -> SuperCallCast()))
  119.         {
  120.             num_arguments = super_call -> NumArguments();
  121.             argument = new AstExpression*[num_arguments + 1];
  122.             for (int i = 0; i < num_arguments; i++)
  123.                 argument[i] = super_call -> Argument(i);
  124.         }
  125.         else
  126.         {
  127.             AstThisCall *this_call = ast -> ThisCallCast();
  128.  
  129.             assert(this_call);
  130.  
  131.             num_arguments = this_call -> NumArguments();
  132.             argument = new AstExpression*[num_arguments + 1];
  133.             for (int i = 0; i < num_arguments; i++)
  134.                 argument[i] = this_call -> Argument(i);
  135.         }
  136.     }
  137.  
  138.     int length = wcslen(name);
  139.  
  140.     for (int i = 0; i < num_arguments; i++)
  141.     {
  142.         TypeSymbol *arg_type = argument[i] -> Type();
  143.         length += arg_type -> ContainingPackage() -> PackageNameLength() +
  144.                   arg_type -> ExternalNameLength() + 3; // '/' after package_name
  145.                                                         // ',' and ' ' to separate this formal parameter from the next one
  146.     }
  147.  
  148.     wchar_t *header = new wchar_t[length + 3]; // +1 for (, +1 for ), +1 for '\0'
  149.     wchar_t *s = header;
  150.  
  151.     for (wchar_t *s2 = name; *s2; s2++)
  152.          *s++ = *s2;
  153.     *s++ = U_LEFT_PARENTHESIS;
  154.     if (num_arguments > 0)
  155.     {
  156.         for (int i = 0; i < num_arguments; i++)
  157.         {
  158.             TypeSymbol *arg_type = argument[i] -> Type();
  159.  
  160.             PackageSymbol *package = arg_type -> ContainingPackage();
  161.             wchar_t *package_name = package -> PackageName();
  162.             if (package -> PackageNameLength() > 0 && wcscmp(package_name, StringConstant::US__DO) != 0)
  163.             {
  164.                 while (*package_name)
  165.                 {
  166.                     *s++ = (*package_name == U_SLASH ? (wchar_t) U_DOT : *package_name);
  167.                     package_name++;
  168.                 }
  169.                 *s++ = U_DOT;
  170.             }
  171.  
  172.             for (wchar_t *s2 = arg_type -> ExternalName(); *s2; s2++)
  173.                 *s++ = (*s2 == U_DOLLAR ? (wchar_t) U_DOT : *s2);
  174.             *s++ = U_COMMA;
  175.             *s++ = U_SPACE;
  176.         }
  177.  
  178.         s -= 2; // remove the last ',' and ' '
  179.     }
  180.     *s++ = U_RIGHT_PARENTHESIS;
  181.     *s = U_NULL;
  182.  
  183.     ReportSemError(kind,
  184.                    ast -> LeftToken(),
  185.                    ast -> RightToken(),
  186.                    header);
  187.  
  188.     delete [] header;
  189.     delete [] argument;
  190.  
  191.     return;
  192. }
  193.  
  194.  
  195. MethodSymbol *Semantic::FindConstructor(TypeSymbol *containing_type, Ast *ast,
  196.                                         LexStream::TokenIndex left_tok, LexStream::TokenIndex right_tok)
  197. {
  198.     Tuple<MethodSymbol *> constructor_set(2);
  199.  
  200.     int num_arguments;
  201.     AstExpression **argument;
  202.  
  203.     AstClassInstanceCreationExpression *class_creation;
  204.     AstSuperCall *super_call;
  205.  
  206.     if ((class_creation = ast -> ClassInstanceCreationExpressionCast()))
  207.     {
  208.         num_arguments = class_creation -> NumArguments();
  209.         argument = new AstExpression*[num_arguments + 1];
  210.         for (int i = 0; i < num_arguments; i++)
  211.             argument[i] = class_creation -> Argument(i);
  212.     }
  213.     else if ((super_call = ast -> SuperCallCast()))
  214.     {
  215.         num_arguments = super_call -> NumArguments();
  216.         argument = new AstExpression*[num_arguments + 1];
  217.         for (int i = 0; i < num_arguments; i++)
  218.             argument[i] = super_call -> Argument(i);
  219.     }
  220.     else
  221.     {
  222.         AstThisCall *this_call = ast -> ThisCallCast();
  223.  
  224.         assert(this_call);
  225.  
  226.         num_arguments = this_call -> NumArguments();
  227.         argument = new AstExpression*[num_arguments + 1];
  228.         for (int i = 0; i < num_arguments; i++)
  229.             argument[i] = this_call -> Argument(i);
  230.     }
  231.  
  232.     assert(containing_type -> ConstructorMembersProcessed());
  233.  
  234.     for (MethodSymbol *constructor = containing_type -> FindConstructorSymbol();
  235.          constructor; constructor = constructor -> next_method)
  236.     {
  237.         if (! constructor -> IsTyped())
  238.             constructor -> ProcessMethodSignature((Semantic *) this, right_tok);
  239.  
  240.         if (num_arguments == constructor -> NumFormalParameters())
  241.         {
  242.             int i;
  243.             for (i = 0; i < num_arguments; i++)
  244.             {
  245.                 if (! CanMethodInvocationConvert(constructor -> FormalParameter(i) -> Type(), argument[i] -> Type()))
  246.                     break;
  247.             }
  248.             if (i == num_arguments)
  249.             {
  250.                 if (MoreSpecific(constructor, constructor_set))
  251.                 {
  252.                     constructor_set.Reset();
  253.                     constructor_set.Next() = constructor;
  254.                 }
  255.                 else if (NoMethodMoreSpecific(constructor_set, constructor))
  256.                     constructor_set.Next() = constructor;
  257.             }
  258.         }
  259.     }
  260.  
  261.     if (constructor_set.Length() == 0)
  262.     {
  263.         MethodSymbol *method;
  264.         for (method = containing_type -> FindMethodSymbol(containing_type -> Identity()); method; method = method -> next_method)
  265.         {
  266.             if (! method -> IsTyped())
  267.                 method -> ProcessMethodSignature((Semantic *) this, right_tok);
  268.  
  269.             if (num_arguments == method -> NumFormalParameters())
  270.             {
  271.                 int i;
  272.                 for (i = 0; i < num_arguments; i++)
  273.                 {
  274.                     if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), argument[i] -> Type()))
  275.                         break;
  276.                 }
  277.                 if (i == num_arguments)
  278.                     break;
  279.             }
  280.         }
  281.  
  282.         if (method)
  283.         {
  284.             if (method -> method_or_constructor_declaration)
  285.             {
  286.                 AstMethodDeclaration *method_declaration = (AstMethodDeclaration *) method -> method_or_constructor_declaration;
  287.                 FileLocation loc(method -> containing_type -> semantic_environment -> sem -> lex_stream,
  288.                                  method_declaration -> method_declarator -> identifier_token);
  289.  
  290.                 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
  291.                                left_tok,
  292.                                right_tok,
  293.                                containing_type -> Name(),
  294.                                loc.location);
  295.             }
  296.             else
  297.             {
  298.                 ReportSemError(SemanticError::METHOD_FOUND_FOR_CONSTRUCTOR,
  299.                                left_tok,
  300.                                right_tok,
  301.                                containing_type -> Name(),
  302.                                method -> containing_type -> file_location -> location);
  303.             }
  304.         }
  305.         else if ((! containing_type -> Bad()) || NumErrors() == 0)
  306.             ReportMethodNotFound(ast, containing_type -> Name());
  307.  
  308.         delete [] argument;
  309.  
  310.         return (MethodSymbol *) NULL;
  311.     }
  312.     else if (constructor_set.Length() > 1)
  313.     {
  314.         ReportSemError(SemanticError::AMBIGUOUS_CONSTRUCTOR_INVOCATION,
  315.                        left_tok,
  316.                        right_tok,
  317.                        containing_type -> Name());
  318.     }
  319.  
  320.     delete [] argument;
  321.  
  322.     MethodSymbol *constructor_symbol = constructor_set[0];
  323.  
  324.     if (constructor_symbol -> IsSynthetic())
  325.     {
  326.         ReportSemError(SemanticError::SYNTHETIC_CONSTRUCTOR_INVOCATION,
  327.                        left_tok,
  328.                        right_tok,
  329.                        constructor_symbol -> Header(),
  330.                        containing_type -> ContainingPackage() -> PackageName(),
  331.                        containing_type -> ExternalName());
  332.     }
  333.  
  334.     //
  335.     // If this constructor came from a class file, make sure that its throws clause has been processed.
  336.     //
  337.     constructor_symbol -> ProcessMethodThrows((Semantic *) this, right_tok);
  338.  
  339.     if (control.option.deprecation &&
  340.         constructor_symbol -> IsDeprecated() &&
  341.         constructor_symbol -> containing_type -> outermost_type != ThisType() -> outermost_type)
  342.     {
  343.         ReportSemError(SemanticError::DEPRECATED_METHOD,
  344.                        left_tok,
  345.                        right_tok,
  346.                        constructor_symbol -> Header(),
  347.                        constructor_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  348.                        constructor_symbol -> containing_type -> ExternalName());
  349.     }
  350.  
  351.     return constructor_symbol;
  352. }
  353.  
  354.  
  355. //
  356. //
  357. //
  358. VariableSymbol *Semantic::FindMisspelledVariableName(TypeSymbol *type, LexStream::TokenIndex identifier_token)
  359. {
  360.     VariableSymbol *misspelled_variable = NULL;
  361.     int index = 0;
  362.     wchar_t *name = lex_stream -> NameString(identifier_token);
  363.  
  364.     for (int k = 0; k < type -> expanded_field_table -> symbol_pool.Length(); k++)
  365.     {
  366.         VariableShadowSymbol *variable_shadow = type -> expanded_field_table -> symbol_pool[k];
  367.         VariableSymbol *variable = variable_shadow -> variable_symbol;
  368.         if (! variable -> IsTyped())
  369.             variable -> ProcessVariableSignature((Semantic *) this, identifier_token);
  370.  
  371.         int new_index = Spell::Index(name, variable -> Name());
  372.         if (new_index > index)
  373.         {
  374.             misspelled_variable = variable;
  375.             index = new_index;
  376.         }
  377.     }
  378.  
  379.     int length = wcslen(name);
  380.  
  381.     return ((length == 3 && index >= 5) || (length == 4 && index >= 6) || (length >= 5 && index >= 7)
  382.                           ? misspelled_variable
  383.                           : (VariableSymbol *) NULL);
  384. }
  385.  
  386. //
  387. //
  388. //
  389. MethodSymbol *Semantic::FindMisspelledMethodName(TypeSymbol *type, AstMethodInvocation *method_call, NameSymbol *name_symbol)
  390. {
  391.     MethodSymbol *misspelled_method = NULL;
  392.     int index = 0;
  393.     AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  394.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  395.     LexStream::TokenIndex identifier_token = (simple_name ? simple_name -> identifier_token : field_access -> identifier_token);
  396.  
  397.     for (int k = 0; k < type -> expanded_method_table -> symbol_pool.Length(); k++)
  398.     {
  399.         MethodShadowSymbol *method_shadow = type -> expanded_method_table -> symbol_pool[k];
  400.         MethodSymbol *method = method_shadow -> method_symbol;
  401.  
  402.         if (! method -> IsTyped())
  403.             method -> ProcessMethodSignature((Semantic *) this, identifier_token);
  404.  
  405.         if (method_call -> NumArguments() == method -> NumFormalParameters())
  406.         {
  407.             int i;
  408.             for (i = 0; i < method_call -> NumArguments(); i++)
  409.             {
  410.                 AstExpression *expr = method_call -> Argument(i);
  411.                 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  412.                     break;
  413.             }
  414.             if (i == method_call -> NumArguments())
  415.             {
  416.                 int new_index = Spell::Index(name_symbol -> Name(), method -> Name());
  417.                 if (new_index > index)
  418.                 {
  419.                     misspelled_method = method;
  420.                     index = new_index;
  421.                 }
  422.             }
  423.         }
  424.     }
  425.  
  426.     int length = name_symbol -> NameLength(),
  427.          num_args = method_call -> NumArguments();
  428.  
  429.     //
  430.     // If we have a name of length 2, accept >= 30% probality if the function takes at least one argument
  431.     // If we have a name of length 3, accept >= 50% probality if the function takes at least one argument
  432.     // Otherwise, if the length of the name is > 3, accept >= 60% probability.
  433.     //
  434.     return (index < 3 ? (MethodSymbol *) NULL
  435.                       : (length == 2 && (index >= 3 || num_args > 0)) ||
  436.                         (length == 3 && (index >= 5 || num_args > 0)) ||
  437.                         (length  > 3 && (index >= 6 || (index >= 5 && num_args > 0)))
  438.                                      ? misspelled_method
  439.                                      : (MethodSymbol *) NULL);
  440. }
  441.  
  442.  
  443. //
  444. // This method is a mirror image of MemberAccessCheck.
  445. //
  446. bool Semantic::IsMethodAccessible(AstFieldAccess *field_access, TypeSymbol *base_type, MethodSymbol *method_symbol)
  447. {
  448.     TypeSymbol *this_type = ThisType(),
  449.                *containing_type = method_symbol -> containing_type;
  450.  
  451.     return (this_type -> outermost_type == containing_type -> outermost_type) ||
  452.             method_symbol -> ACC_PUBLIC() ||
  453.             ((! method_symbol -> ACC_PRIVATE()) && containing_type -> ContainingPackage() == this_package) ||
  454.             (method_symbol -> ACC_PROTECTED() && (field_access -> base -> IsSuperExpression() ||
  455.                                                   (this_type -> HasProtectedAccessTo(containing_type) &&
  456.                                                    (base_type -> IsSubclass(this_type) || base_type -> IsOwner(this_type)))));
  457. }
  458.  
  459.  
  460. //
  461. // Search the type in question for a method. Note that name_symbol is an optional argument.
  462. // If it was not passed to this function then its default value is NULL (see semantic.h) and
  463. // we assume that the name to search for is the name specified in the field_access of the
  464. // method_call.
  465. //
  466. MethodSymbol *Semantic::FindMethodInType(TypeSymbol *type, AstMethodInvocation *method_call, NameSymbol *name_symbol)
  467. {
  468.     Tuple<MethodSymbol *> method_set(2);
  469.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  470.     if (! name_symbol)
  471.         name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  472.  
  473.     if (! type -> expanded_method_table)
  474.         ComputeMethodsClosure(type, field_access -> identifier_token);
  475.  
  476. //
  477. // TODO: Confirm that this is no longer the case as of javac 1.2
  478. //
  479. /*
  480.     //
  481.     // First look for the method in the "type". If it is not found, look for
  482.     // it in the superclasses in the proper order. It is possible that the
  483.     // method in question is a private field that is contained in the body
  484.     // of the type that we are currently processing (this_type()), in which
  485.     // case it is accessible even though it is not directly inherited by "type".
  486.     //
  487.     for (TypeSymbol *type_symbol = type; type_symbol && method_set.Length() == 0; type_symbol = type_symbol -> super)
  488.     {
  489. */
  490.         TypeSymbol *type_symbol = type;
  491.  
  492.         for (MethodShadowSymbol *method_shadow = type_symbol -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  493.              method_shadow; method_shadow = method_shadow -> next_method)
  494.         {
  495.             MethodSymbol *method = method_shadow -> method_symbol;
  496.  
  497.             if (! method -> IsTyped())
  498.                 method -> ProcessMethodSignature((Semantic *) this, field_access -> identifier_token);
  499.  
  500.             if (method_call -> NumArguments() == method -> NumFormalParameters() &&
  501.                 IsMethodAccessible(field_access, type_symbol, method))
  502.             {
  503.                 int i;
  504.                 for (i = 0; i < method_call -> NumArguments(); i++)
  505.                 {
  506.                     AstExpression *expr = method_call -> Argument(i);
  507.                     if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  508.                         break;
  509.                 }
  510.                 if (i == method_call -> NumArguments())
  511.                 {
  512.                     if (MoreSpecific(method, method_set))
  513.                     {
  514.                         method_set.Reset();
  515.                         method_set.Next() = method;
  516.                     }
  517.                     else if (NoMethodMoreSpecific(method_set, method))
  518.                         method_set.Next() = method;
  519.                 }
  520.             }
  521.         }
  522.  
  523. /*
  524. See comment above...
  525.     }
  526. */
  527.  
  528.     if (method_set.Length() == 0)
  529.     {
  530.         if (! type -> expanded_field_table)
  531.             ComputeFieldsClosure(type, field_access -> identifier_token);
  532.  
  533.         VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  534.  
  535.         if (variable_shadow_symbol)
  536.         {
  537.             VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  538.             TypeSymbol *enclosing_type = variable_symbol -> owner -> TypeCast();
  539.  
  540.             assert(enclosing_type);
  541.  
  542.             ReportSemError(SemanticError::FIELD_NOT_METHOD,
  543.                            method_call -> LeftToken(),
  544.                            method_call -> RightToken(),
  545.                            variable_symbol -> Name(),
  546.                            enclosing_type -> ContainingPackage() -> PackageName(),
  547.                            enclosing_type -> ExternalName());
  548.         }
  549.         else
  550.         {
  551.             TypeSymbol *super_type;
  552.             MethodShadowSymbol *method_shadow;
  553.  
  554.             //
  555.             // Check whether or not the method we are looking for is not an inaccessible private method.
  556.             //
  557.             for (super_type = type; super_type; super_type = super_type -> super)
  558.             {
  559.                 for (method_shadow = super_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  560.                      method_shadow; method_shadow = method_shadow -> next_method)
  561.                 {
  562.                     MethodSymbol *method = method_shadow -> method_symbol;
  563.                     if (! method -> IsTyped())
  564.                         method -> ProcessMethodSignature((Semantic *) this, field_access -> identifier_token);
  565.  
  566.                     if (method_call -> NumArguments() == method -> NumFormalParameters())
  567.                     {
  568.                         int i;
  569.                         for (i = 0; i < method_call -> NumArguments(); i++)
  570.                         {
  571.                             AstExpression *expr = method_call -> Argument(i);
  572.                             if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  573.                                 break;
  574.                         }
  575.                         if (i == method_call -> NumArguments()) // found a match ?
  576.                             break;
  577.                     }
  578.                 }
  579.  
  580.                 if (method_shadow) // found a match ?
  581.                     break;
  582.             }
  583.  
  584.             if (super_type)
  585.             {
  586.                 ReportSemError((method_shadow -> method_symbol -> ACC_PRIVATE()
  587.                                                ? SemanticError::METHOD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  588.                                                : SemanticError::METHOD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  589.                                method_call -> LeftToken(),
  590.                                method_call -> RightToken(),
  591.                                method_shadow -> method_symbol -> Header(),
  592.                                super_type -> ContainingPackage() -> PackageName(),
  593.                                super_type -> ExternalName(),
  594.                                ThisType() -> ContainingPackage() -> PackageName(),
  595.                                ThisType() -> ExternalName());
  596.             }
  597.             else
  598.             {
  599.                 if (FindNestedType(type, field_access -> identifier_token))
  600.                 {
  601.                     ReportSemError(SemanticError::TYPE_NOT_METHOD,
  602.                                    field_access -> identifier_token,
  603.                                    field_access -> identifier_token,
  604.                                    name_symbol -> Name());
  605.                 }
  606.                 else if (type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol))
  607.                     ReportMethodNotFound(method_call, name_symbol -> Name());
  608.                 else
  609.                 {
  610.                     MethodSymbol *method = FindMisspelledMethodName(type, method_call, name_symbol);
  611.                     if (method)
  612.                          ReportSemError(SemanticError::METHOD_NAME_MISSPELLED,
  613.                                         method_call -> LeftToken(),
  614.                                         method_call -> RightToken(),
  615.                                         name_symbol -> Name(),
  616.                                         type -> ContainingPackage() -> PackageName(),
  617.                                         type -> ExternalName(),
  618.                                         method -> Name());
  619.                     else ReportSemError(SemanticError::METHOD_NAME_NOT_FOUND_IN_TYPE,
  620.                                         method_call -> LeftToken(),
  621.                                         method_call -> RightToken(),
  622.                                         name_symbol -> Name(),
  623.                                         type -> ContainingPackage() -> PackageName(),
  624.                                         type -> ExternalName());
  625.                 }
  626.             }
  627.         }
  628.  
  629.         return (MethodSymbol *) NULL;
  630.     }
  631.     else if (method_set.Length() > 1)
  632.     {
  633.         ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
  634.                        method_call -> LeftToken(),
  635.                        method_call -> RightToken(),
  636.                        name_symbol -> Name(),
  637.                        method_set[0] -> Header(),
  638.                        method_set[0] -> containing_type -> ContainingPackage() -> PackageName(),
  639.                        method_set[0] -> containing_type -> ExternalName(),
  640.                        method_set[1] -> Header(),
  641.                        method_set[1] -> containing_type -> ContainingPackage() -> PackageName(),
  642.                        method_set[1] -> containing_type -> ExternalName());
  643.     }
  644.  
  645.     MethodSymbol *method = method_set[0];
  646.     if (method -> IsSynthetic())
  647.     {
  648.         ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
  649.                        method_call -> LeftToken(),
  650.                        method_call -> RightToken(),
  651.                        method -> Header(),
  652.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  653.                        method -> containing_type -> ExternalName());
  654.     }
  655.  
  656.     //
  657.     // If this method came from a class file, make sure that its throws clause has been processed.
  658.     //
  659.     method -> ProcessMethodThrows((Semantic *) this, field_access -> identifier_token);
  660.  
  661.     if (control.option.deprecation &&
  662.         method -> IsDeprecated() && method -> containing_type -> outermost_type != ThisType() -> outermost_type)
  663.     {
  664.         ReportSemError(SemanticError::DEPRECATED_METHOD,
  665.                        method_call -> LeftToken(),
  666.                        method_call -> RightToken(),
  667.                        method -> Header(),
  668.                        method -> containing_type -> ContainingPackage() -> PackageName(),
  669.                        method -> containing_type -> ExternalName());
  670.     }
  671.  
  672.     return method;
  673. }
  674.  
  675.  
  676. void Semantic::SearchForMethodInEnvironment(Tuple<MethodSymbol *> &methods_found,
  677.                                             SemanticEnvironment *&where_found,
  678.                                             SemanticEnvironment *stack,
  679.                                             AstMethodInvocation *method_call)
  680. {
  681.     AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  682.     NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  683.  
  684.     for (SemanticEnvironment *env = stack; env; env = env -> previous)
  685.     {
  686.         TypeSymbol *type = env -> Type();
  687.         if (! type -> expanded_method_table)
  688.             ComputeMethodsClosure(type, simple_name -> identifier_token);
  689.  
  690.         methods_found.Reset();
  691.         where_found = NULL;
  692.  
  693.         //
  694.         // If this environment contained a method with the right name, the search stops:
  695.         //
  696.         //    "Class scoping does not influence overloading: if the inner class has one
  697.         //     print method, the simple method name 'print' refers to that method, not
  698.         //     any of the ten 'print' methods in the enclosing class."
  699.         //
  700.         MethodShadowSymbol *method_shadow = type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  701.         if (method_shadow)
  702.         {
  703.             for (; method_shadow; method_shadow = method_shadow -> next_method)
  704.             {
  705.                 MethodSymbol *method = method_shadow -> method_symbol;
  706.  
  707.                 if (! method -> IsTyped())
  708.                     method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  709.  
  710.                 //
  711.                 // Since type -> IsOwner(this_type()), i.e., type encloses this_type(),
  712.                 // method is accessible, even if it is private.
  713.                 //
  714.                 if (method_call -> NumArguments() == method -> NumFormalParameters())
  715.                 {
  716.                     int i;
  717.                     for (i = 0; i < method_call -> NumArguments(); i++)
  718.                     {
  719.                         AstExpression *expr = method_call -> Argument(i);
  720.                         if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  721.                             break;
  722.                     }
  723.                     if (i == method_call -> NumArguments())
  724.                     {
  725.                         if (MoreSpecific(method, methods_found))
  726.                         {
  727.                             methods_found.Reset();
  728.                             methods_found.Next() = method;
  729.                         }
  730.                         else if (NoMethodMoreSpecific(methods_found, method))
  731.                             methods_found.Next() = method;
  732.                     }
  733.                 }
  734.             }
  735.  
  736.             //
  737.             // If a match was found, save the environment
  738.             //
  739.             where_found = (methods_found.Length() > 0 ? env : (SemanticEnvironment *) NULL);
  740.             break;
  741.         }
  742.     }
  743.  
  744.     return;
  745. }
  746.  
  747.  
  748. MethodSymbol *Semantic::FindMethodInEnvironment(SemanticEnvironment *&where_found,
  749.                                                 SemanticEnvironment *stack,
  750.                                                 AstMethodInvocation *method_call)
  751. {
  752.     Tuple<MethodSymbol *> methods_found(2);
  753.     SearchForMethodInEnvironment(methods_found, where_found, stack, method_call);
  754.  
  755.     MethodSymbol *method_symbol = (methods_found.Length() > 0 ? methods_found[0] : (MethodSymbol *) NULL);
  756.     if (method_symbol)
  757.     {
  758.         for (int i = 1; i < methods_found.Length(); i++)
  759.         {
  760.             ReportSemError(SemanticError::AMBIGUOUS_METHOD_INVOCATION,
  761.                            method_call -> LeftToken(),
  762.                            method_call -> RightToken(),
  763.                            method_symbol -> Name(),
  764.                            methods_found[0] -> Header(),
  765.                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  766.                            method_symbol -> containing_type -> ExternalName(),
  767.                            methods_found[i] -> Header(),
  768.                            methods_found[i] -> containing_type -> ContainingPackage() -> PackageName(),
  769.                            methods_found[i] -> containing_type -> ExternalName());
  770.         }
  771.  
  772.         if (method_symbol -> containing_type != where_found -> Type())  // is symbol an inherited field?
  773.         {
  774.             if (method_symbol -> IsSynthetic())
  775.             {
  776.                 ReportSemError(SemanticError::SYNTHETIC_METHOD_INVOCATION,
  777.                                method_call -> LeftToken(),
  778.                                method_call -> RightToken(),
  779.                                method_symbol -> Header(),
  780.                                method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  781.                                method_symbol -> containing_type -> ExternalName());
  782.             }
  783.             else if (! where_found -> Type() -> ACC_STATIC())
  784.             {
  785.                 Tuple<MethodSymbol *> others(2);
  786.                 SemanticEnvironment *found_other,
  787.                                     *previous_env = where_found -> previous;
  788.                 SearchForMethodInEnvironment(others, found_other, previous_env, method_call);
  789.  
  790.                 if (others.Length() > 0 && where_found -> Type() -> CanAccess(found_other -> Type()))
  791.                 {
  792.                     for (int i = 0; i < others.Length();  i++)
  793.                     {
  794.                         if (! (others[i] == method_symbol && method_symbol -> ACC_STATIC()))
  795.                         {
  796.                             ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
  797.                                            method_call -> LeftToken(),
  798.                                            method_call -> RightToken(),
  799.                                            method_symbol -> Name(),
  800.                                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  801.                                            method_symbol -> containing_type -> ExternalName(),
  802.                                            found_other -> Type() -> ContainingPackage() -> PackageName(),
  803.                                            found_other -> Type() -> ExternalName());
  804.                             break; // emit only one error message
  805.                         }
  806.                     }
  807.                 }
  808.             }
  809.         }
  810.     }
  811.     else
  812.     {
  813.         AstSimpleName *simple_name = method_call -> method -> SimpleNameCast();
  814.         NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  815.         bool symbol_found = false;
  816.  
  817.         //
  818.         // First, search for a perfect visible method match in an enclosing class.
  819.         //
  820.         assert(stack);
  821.         for (SemanticEnvironment *env = stack -> previous; env; env = env -> previous)
  822.         {
  823.             Tuple<MethodSymbol *> others(2);
  824.             SemanticEnvironment *found_other;
  825.             SearchForMethodInEnvironment(others, found_other, env, method_call);
  826.  
  827.             if (others.Length() > 0)
  828.             {
  829.                 ReportSemError(SemanticError::HIDDEN_METHOD_IN_ENCLOSING_CLASS,
  830.                                method_call -> LeftToken(),
  831.                                method_call -> RightToken(),
  832.                                others[0] -> Header(),
  833.                                others[0] -> containing_type -> ContainingPackage() -> PackageName(),
  834.                                others[0] -> containing_type -> ExternalName());
  835.  
  836.                 symbol_found = true;
  837.                 break;
  838.             }
  839.         }
  840.  
  841.         //
  842.         // If a method in an enclosing class was not found. Search for a similar visible field
  843.         // or a private method with the name.
  844.         //
  845.         for (SemanticEnvironment *env2 = stack; env2 && (! symbol_found); env2 = env2 -> previous)
  846.         {
  847.             TypeSymbol *type = env2 -> Type();
  848.  
  849.             if (! type -> expanded_field_table)
  850.                 ComputeFieldsClosure(type, simple_name -> identifier_token);
  851.  
  852.             VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  853.             if (variable_shadow_symbol)
  854.             {
  855.                 VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  856.                 TypeSymbol *enclosing_type = variable_symbol -> owner -> TypeCast();
  857.  
  858.                 assert(enclosing_type);
  859.  
  860.                 ReportSemError(SemanticError::FIELD_NOT_METHOD,
  861.                                method_call -> LeftToken(),
  862.                                method_call -> RightToken(),
  863.                                variable_symbol -> Name(),
  864.                                enclosing_type -> ContainingPackage() -> PackageName(),
  865.                                enclosing_type -> ExternalName());
  866.                 symbol_found = true;
  867.                 break;
  868.             }
  869.             else
  870.             {
  871.                 TypeSymbol *super_type;
  872.                 MethodShadowSymbol *method_shadow;
  873.  
  874.                 for (super_type = type -> super; super_type; super_type = super_type -> super)
  875.                 {
  876.                     for (method_shadow = super_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  877.                          method_shadow; method_shadow = method_shadow -> next_method)
  878.                     {
  879.                         MethodSymbol *method = method_shadow -> method_symbol;
  880.                         if (! method -> IsTyped())
  881.                             method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  882.  
  883.                         if (method_call -> NumArguments() == method -> NumFormalParameters())
  884.                         {
  885.                             int i;
  886.                             for (i = 0; i < method_call -> NumArguments(); i++)
  887.                             {
  888.                                 AstExpression *expr = method_call -> Argument(i);
  889.                                 if (! CanMethodInvocationConvert(method -> FormalParameter(i) -> Type(), expr -> Type()))
  890.                                     break;
  891.                             }
  892.                             if (i == method_call -> NumArguments()) // found a match ?
  893.                                 break;
  894.                         }
  895.                     }
  896.  
  897.                     if (method_shadow) // found a match ?
  898.                         break;
  899.                 }
  900.  
  901.                 if (super_type)
  902.                 {
  903.                     ReportSemError((method_shadow -> method_symbol -> ACC_PRIVATE()
  904.                                                    ? SemanticError::METHOD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  905.                                                    : SemanticError::METHOD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  906.                                    method_call -> LeftToken(),
  907.                                    method_call -> RightToken(),
  908.                                    name_symbol -> Name(),
  909.                                    super_type -> ContainingPackage() -> PackageName(),
  910.                                    super_type -> ExternalName(),
  911.                                    type -> ContainingPackage() -> PackageName(),
  912.                                    type -> ExternalName());
  913.                     symbol_found = true;
  914.                     break;
  915.                 }
  916.             }
  917.         }
  918.  
  919.         //
  920.         // Finally, if we did not find a method or field name that matches, look for a type
  921.         // with that name.
  922.         //
  923.         if (! symbol_found)
  924.         {
  925.             TypeSymbol *this_type = ThisType();
  926.  
  927.             if (FindType(simple_name -> identifier_token))
  928.             {
  929.                 ReportSemError(SemanticError::TYPE_NOT_METHOD,
  930.                                simple_name -> identifier_token,
  931.                                simple_name -> identifier_token,
  932.                                name_symbol -> Name());
  933.             }
  934.             else if (this_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol))
  935.                 ReportMethodNotFound(method_call, name_symbol -> Name());
  936.             else
  937.             {
  938.                 MethodSymbol *method = FindMisspelledMethodName(this_type, method_call, name_symbol);
  939.                 if (method)
  940.                      ReportSemError(SemanticError::METHOD_NAME_MISSPELLED,
  941.                                     method_call -> LeftToken(),
  942.                                     method_call -> RightToken(),
  943.                                     name_symbol -> Name(),
  944.                                     this_type -> ContainingPackage() -> PackageName(),
  945.                                     this_type -> ExternalName(),
  946.                                     method -> Name());
  947.                 else ReportSemError(SemanticError::METHOD_NAME_NOT_FOUND_IN_TYPE,
  948.                                     method_call -> LeftToken(),
  949.                                     method_call -> RightToken(),
  950.                                     name_symbol -> Name(),
  951.                                     this_type -> ContainingPackage() -> PackageName(),
  952.                                     this_type -> ExternalName());
  953.             }
  954.         }
  955.     }
  956.  
  957.     //
  958.     // If this method came from a class file, make sure that its throws clause has been processed.
  959.     //
  960.     if (method_symbol)
  961.     {
  962.         method_symbol -> ProcessMethodThrows((Semantic *) this, method_call -> method -> RightToken());
  963.  
  964.         if (control.option.deprecation &&
  965.             method_symbol -> IsDeprecated() && method_symbol -> containing_type -> outermost_type != ThisType() -> outermost_type)
  966.         {
  967.             ReportSemError(SemanticError::DEPRECATED_METHOD,
  968.                            method_call -> LeftToken(),
  969.                            method_call -> RightToken(),
  970.                            method_symbol -> Header(),
  971.                            method_symbol -> containing_type -> ContainingPackage() -> PackageName(),
  972.                            method_symbol -> containing_type -> ExternalName());
  973.         }
  974.     }
  975.  
  976.     return method_symbol;
  977. }
  978.  
  979.  
  980.  
  981. //
  982. // Search the type in question for a variable. Note that name_symbol is an optional argument.
  983. // If it was not passed to this function then its default value is NULL (see semantic.h) and
  984. // we assume that the name to search for is the last identifier specified in the field_access.
  985. //
  986. inline VariableSymbol *Semantic::FindVariableInType(TypeSymbol *type, AstFieldAccess *field_access, NameSymbol *name_symbol)
  987. {
  988.     if (! name_symbol)
  989.         name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  990.  
  991.     if (! type -> expanded_field_table)
  992.         ComputeFieldsClosure(type, field_access -> identifier_token);
  993.  
  994. //
  995. // TODO: Confirm that this is no longer the case as of javac 1.2
  996. //
  997. /*
  998.     //
  999.     // First look for the variable in the "type". If it is not found, look for
  1000.     // it in the superclasses in the proper order. It is possible that the
  1001.     // field in question is a private field that is contained in the body
  1002.     // of the type that we are currently processing (this_type()), in which case
  1003.     // it is accessible even though it is not directly inherited by "type".
  1004.     //
  1005.     VariableShadowSymbol *variable_shadow_symbol;
  1006.     for (variable_shadow_symbol = NULL; (! variable_shadow_symbol) && type; type = type -> super)
  1007.         variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1008. */
  1009.  
  1010.     VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1011.  
  1012.     //
  1013.     // Recall that even an inaccessible member x of a super class (or interface) S,
  1014.     // in addition to not been inherited by a subclass, hides all other occurrences of x that may
  1015.     // appear in a super class (or super interface) of S (see 8.3).
  1016.     //
  1017.     if (variable_shadow_symbol)
  1018.     {
  1019.         VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  1020.  
  1021.         for (int i = 0; i < variable_shadow_symbol -> NumConflicts(); i++)
  1022.         {
  1023.             ReportSemError(SemanticError::AMBIGUOUS_FIELD,
  1024.                            field_access -> LeftToken(),
  1025.                            field_access -> RightToken(),
  1026.                            name_symbol -> Name(),
  1027.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1028.                            variable_symbol -> owner -> TypeCast() -> ExternalName(),
  1029.                            variable_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1030.                            variable_shadow_symbol -> Conflict(i) -> owner -> TypeCast() -> ExternalName());
  1031.         }
  1032.  
  1033.         if (variable_symbol -> IsSynthetic())
  1034.         {
  1035.             ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS,
  1036.                            field_access -> LeftToken(),
  1037.                            field_access -> RightToken(),
  1038.                            variable_symbol -> Name(),
  1039.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1040.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1041.         }
  1042.  
  1043.         if (control.option.deprecation &&
  1044.             variable_symbol -> IsDeprecated() &&
  1045.             variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  1046.         {
  1047.             ReportSemError(SemanticError::DEPRECATED_FIELD,
  1048.                            field_access -> LeftToken(),
  1049.                            field_access -> RightToken(),
  1050.                            variable_symbol -> Name(),
  1051.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1052.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1053.         }
  1054.  
  1055.         if (! variable_symbol -> IsTyped())
  1056.             variable_symbol -> ProcessVariableSignature((Semantic *) this, field_access -> identifier_token);
  1057.  
  1058.         return variable_symbol;
  1059.     }
  1060.  
  1061.     return (VariableSymbol *) NULL;
  1062. }
  1063.  
  1064.  
  1065. void Semantic::ReportAccessedFieldNotFound(AstFieldAccess *field_access, TypeSymbol *type)
  1066. {
  1067.     NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  1068.     VariableShadowSymbol *variable_shadow_symbol;
  1069.  
  1070.     if (! type -> expanded_field_table)
  1071.         ComputeFieldsClosure(type, field_access -> base -> LeftToken());
  1072.     TypeSymbol *super_type;
  1073.     for (super_type = type -> super; super_type; super_type = super_type -> super)
  1074.     {
  1075.         variable_shadow_symbol = super_type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1076.         if (variable_shadow_symbol)
  1077.             break;
  1078.     }
  1079.  
  1080.     if (super_type)
  1081.     {
  1082.         VariableSymbol *variable_symbol = variable_shadow_symbol -> variable_symbol;
  1083.         ReportSemError((variable_symbol -> ACC_PRIVATE()
  1084.                                          ? SemanticError::FIELD_WITH_PRIVATE_ACCESS_NOT_ACCESSIBLE
  1085.                                          : SemanticError::FIELD_WITH_DEFAULT_ACCESS_NOT_ACCESSIBLE),
  1086.                        field_access -> LeftToken(),
  1087.                        field_access -> RightToken(),
  1088.                        variable_symbol -> Name(),
  1089.                        super_type -> ContainingPackage() -> PackageName(),
  1090.                        super_type -> ExternalName(),
  1091.                        type -> ContainingPackage() -> PackageName(),
  1092.                        type -> ExternalName());
  1093.     }
  1094.     else
  1095.     {
  1096.         VariableSymbol *variable = FindMisspelledVariableName(type, field_access -> identifier_token);
  1097.         if (variable)
  1098.              ReportSemError(SemanticError::FIELD_NAME_MISSPELLED,
  1099.                             field_access -> LeftToken(),
  1100.                             field_access -> RightToken(),
  1101.                             name_symbol -> Name(),
  1102.                             type -> ContainingPackage() -> PackageName(),
  1103.                             type -> ExternalName(),
  1104.                             variable -> Name());
  1105.         else ReportSemError(SemanticError::FIELD_NOT_FOUND,
  1106.                             field_access -> LeftToken(),
  1107.                             field_access -> RightToken(),
  1108.                             lex_stream -> NameString(field_access -> identifier_token),
  1109.                             type -> ContainingPackage() -> PackageName(),
  1110.                             type -> ExternalName());
  1111.     }
  1112.  
  1113.     return;
  1114. }
  1115.  
  1116.  
  1117. void Semantic::SearchForVariableInEnvironment(Tuple<VariableSymbol *> &variables_found,
  1118.                                               SemanticEnvironment *&where_found,
  1119.                                               SemanticEnvironment *stack,
  1120.                                               NameSymbol *name_symbol,
  1121.                                               LexStream::TokenIndex identifier_token)
  1122. {
  1123.     variables_found.Reset();
  1124.     where_found = (SemanticEnvironment *) NULL;
  1125.  
  1126.     for (SemanticEnvironment *env = stack; env; env = env -> previous)
  1127.     {
  1128.         VariableSymbol *variable_symbol = env -> symbol_table.FindVariableSymbol(name_symbol);
  1129.         if (variable_symbol) // a local variable
  1130.         {
  1131.             variables_found.Next() = variable_symbol;
  1132.             where_found = env;
  1133.             break;
  1134.         }
  1135.  
  1136.         TypeSymbol *type = env -> Type();
  1137.         if (! type -> expanded_field_table)
  1138.             ComputeFieldsClosure(type, identifier_token);
  1139.         VariableShadowSymbol *variable_shadow_symbol = type -> expanded_field_table -> FindVariableShadowSymbol(name_symbol);
  1140.         if (variable_shadow_symbol)
  1141.         {
  1142.             //
  1143.             // Since type -> IsOwner(this_type()), i.e., type encloses this_type(),
  1144.             // variable_symbol is accessible, even if it is private.
  1145.             //
  1146.             variables_found.Next() = variable_shadow_symbol -> variable_symbol;
  1147.  
  1148.             //
  1149.             // Recall that even an inaccessible member x of a super class (or interface) S,
  1150.             // in addition to not been inherited by a subclass, hides all other occurrences of x that may
  1151.             // appear in a super class (or super interface) of S (see 8.3).
  1152.             //
  1153.             for (int i = 0; i < variable_shadow_symbol -> NumConflicts(); i++)
  1154.                 variables_found.Next() = variable_shadow_symbol -> Conflict(i);
  1155.             where_found = env;
  1156.             break;
  1157.         }
  1158.     }
  1159.  
  1160.     return;
  1161. }
  1162.  
  1163.  
  1164. VariableSymbol *Semantic::FindVariableInEnvironment(SemanticEnvironment *&where_found,
  1165.                                                     SemanticEnvironment *stack, LexStream::TokenIndex identifier_token)
  1166. {
  1167.     Tuple<VariableSymbol *> variables_found(2);
  1168.     NameSymbol *name_symbol = lex_stream -> NameSymbol(identifier_token);
  1169.     SearchForVariableInEnvironment(variables_found, where_found, stack, name_symbol, identifier_token);
  1170.  
  1171.     VariableSymbol *variable_symbol = (VariableSymbol *) (variables_found.Length() > 0 ? variables_found[0] : NULL);
  1172.  
  1173.     if (variable_symbol)
  1174.     {
  1175.         if (variable_symbol -> IsLocal()) // a local variable
  1176.         {
  1177.             if (where_found != stack)
  1178.             {
  1179.                 TypeSymbol *type = stack -> Type();
  1180.  
  1181.                 if (! variable_symbol -> ACC_FINAL())
  1182.                 {
  1183.                     MethodSymbol *method = variable_symbol -> owner -> MethodCast();
  1184.  
  1185.                     ReportSemError(SemanticError::INNER_CLASS_REFERENCE_TO_NON_FINAL_LOCAL_VARIABLE,
  1186.                                    identifier_token,
  1187.                                    identifier_token,
  1188.                                    type -> ContainingPackage() -> PackageName(),
  1189.                                    type -> ExternalName(),
  1190.                                    lex_stream -> NameString(identifier_token),
  1191.                                    //
  1192.                                    // TODO: What if the method is a constructor ?
  1193.                                    //        if (method -> Identity() != control.init_symbol &&
  1194.                                    //            method -> Identity() != control.block_init_symbol &&
  1195.                                    //            method -> Identity() != control.clinit_symbol)
  1196.                                    //
  1197.                                    //
  1198.                                    method -> Name());
  1199.                 }
  1200.  
  1201.                 //
  1202.                 // Insert a local shadow in the type. If we are currently processing a
  1203.                 // constructor, the local shadow would have been passed to it as an argument.
  1204.                 // If so, use the local argument; otherwise, use the local shadow.
  1205.                 //
  1206.                 VariableSymbol *local_shadow = type -> FindOrInsertLocalShadow(variable_symbol),
  1207.                                *local_symbol = stack -> symbol_table.FindVariableSymbol(local_shadow -> Identity());
  1208.                 variable_symbol = (local_symbol ? local_symbol : local_shadow);
  1209.  
  1210.                 assert(variable_symbol);
  1211.  
  1212.                 where_found = stack;
  1213.             }
  1214.         }
  1215.         else if (variable_symbol -> owner != where_found -> Type())  // is symbol an inherited field?
  1216.         {
  1217.             TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  1218.  
  1219.             if (variable_symbol -> IsSynthetic())
  1220.             {
  1221.                 ReportSemError(SemanticError::SYNTHETIC_VARIABLE_ACCESS,
  1222.                                identifier_token,
  1223.                                identifier_token,
  1224.                                variable_symbol -> Name(),
  1225.                                type -> ContainingPackage() -> PackageName(),
  1226.                                type -> ExternalName());
  1227.             }
  1228.             else if (! where_found -> Type() -> ACC_STATIC())
  1229.             {
  1230.                 Tuple<VariableSymbol *> others(2);
  1231.                 SemanticEnvironment *found_other,
  1232.                                     *previous_env = where_found -> previous;
  1233.                 SearchForVariableInEnvironment(others, found_other, previous_env, name_symbol, identifier_token);
  1234.  
  1235.                 if (others.Length() > 0 && where_found -> Type() -> CanAccess(found_other -> Type()))
  1236.                 {
  1237.                     for (int i = 0; i < others.Length();  i++)
  1238.                     {
  1239.                         if (! (others[i] == variable_symbol && variable_symbol -> ACC_STATIC()))
  1240.                         {
  1241.                             MethodSymbol *method = others[i] -> owner -> MethodCast();
  1242.  
  1243.                             if (method)
  1244.                             {
  1245.                                 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_LOCAL,
  1246.                                                identifier_token,
  1247.                                                identifier_token,
  1248.                                                lex_stream -> NameString(identifier_token),
  1249.                                                type -> ContainingPackage() -> PackageName(),
  1250.                                                type -> ExternalName(),
  1251.                                                method -> Name());
  1252.                                 break;
  1253.                             }
  1254.                             else
  1255.                             {
  1256.                                 ReportSemError(SemanticError::INHERITANCE_AND_LEXICAL_SCOPING_CONFLICT_WITH_MEMBER,
  1257.                                                identifier_token,
  1258.                                                identifier_token,
  1259.                                                lex_stream -> NameString(identifier_token),
  1260.                                                type -> ContainingPackage() -> PackageName(),
  1261.                                                type -> ExternalName(),
  1262.                                                found_other -> Type() -> ContainingPackage() -> PackageName(),
  1263.                                                found_other -> Type() -> ExternalName());
  1264.                                 break;
  1265.                             }
  1266.                         }
  1267.                     }
  1268.                 }
  1269.             }
  1270.         }
  1271.     }
  1272.  
  1273.     for (int i = 1; i < variables_found.Length(); i++)
  1274.     {
  1275.         ReportSemError(SemanticError::AMBIGUOUS_FIELD,
  1276.                        identifier_token,
  1277.                        identifier_token,
  1278.                        variable_symbol -> Name(),
  1279.                        variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1280.                        variable_symbol -> owner -> TypeCast() -> ExternalName(),
  1281.                        variables_found[i] -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1282.                        variables_found[i] -> owner -> TypeCast() -> ExternalName());
  1283.     }
  1284.  
  1285.     if (variable_symbol)
  1286.     {
  1287.         if (control.option.deprecation &&
  1288.             variable_symbol -> IsDeprecated() &&
  1289.             variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  1290.         {
  1291.             ReportSemError(SemanticError::DEPRECATED_FIELD,
  1292.                            identifier_token,
  1293.                            identifier_token,
  1294.                            variable_symbol -> Name(),
  1295.                            variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  1296.                            variable_symbol -> owner -> TypeCast() -> ExternalName());
  1297.         }
  1298.  
  1299.         if (! variable_symbol -> IsTyped())
  1300.             variable_symbol -> ProcessVariableSignature((Semantic *) this, identifier_token);
  1301.     }
  1302.  
  1303.     return variable_symbol;
  1304. }
  1305.  
  1306.  
  1307. VariableSymbol *Semantic::FindInstance(TypeSymbol *base_type, TypeSymbol *environment_type)
  1308. {
  1309.     for (int i = 0; i < base_type -> NumEnclosingInstances(); i++)
  1310.     {
  1311.         VariableSymbol *variable = base_type -> EnclosingInstance(i);
  1312.         if (variable -> Type() -> IsSubclass(environment_type))
  1313.             return variable;
  1314.     }
  1315.  
  1316.     AstClassDeclaration *class_declaration = base_type -> declaration -> ClassDeclarationCast();
  1317.     AstClassInstanceCreationExpression *class_creation = base_type -> declaration -> ClassInstanceCreationExpressionCast();
  1318.  
  1319.     assert(class_declaration || class_creation);
  1320.  
  1321.     AstClassBody *class_body = (class_declaration ? class_declaration -> class_body : class_creation -> class_body_opt);
  1322.  
  1323.     LexStream::TokenIndex loc = class_body -> left_brace_token;
  1324.     AstBlock *this_block = class_body -> this_block;
  1325.     if (! this_block)
  1326.     {
  1327.         this_block = compilation_unit -> ast_pool -> GenBlock();
  1328.         this_block -> left_brace_token  = loc;
  1329.         this_block -> right_brace_token = loc;
  1330.  
  1331.         this_block -> is_reachable = true;
  1332.         this_block -> can_complete_normally = true;
  1333.  
  1334.         class_body -> this_block = this_block;
  1335.     }
  1336.  
  1337.     int k = base_type -> NumEnclosingInstances();
  1338.     for (TypeSymbol *type = base_type -> EnclosingInstance(k - 1) -> Type(); type; type = type -> ContainingType(), k++)
  1339.     {
  1340.         AstSimpleName *parm = compilation_unit -> ast_pool -> GenSimpleName(loc);
  1341.         parm -> symbol = base_type -> EnclosingInstance(k - 1);
  1342.  
  1343.         VariableSymbol *variable_symbol = (VariableSymbol *) type -> FindThis(0);
  1344.  
  1345.         AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  1346.         method_name -> base = parm;
  1347.         method_name -> dot_token = loc;
  1348.         method_name -> identifier_token = loc;
  1349.         method_name -> symbol = variable_symbol; // the variable in question
  1350.  
  1351.         AstMethodInvocation *rhs       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1352.         rhs -> method                  = method_name;
  1353.         rhs -> left_parenthesis_token  = loc;
  1354.         rhs -> right_parenthesis_token = loc;
  1355.         rhs -> symbol                  = type -> GetReadAccessMethod(variable_symbol);
  1356.         rhs -> AddArgument(parm); // TODO: WARNING: sharing of Ast subtree !!!
  1357.  
  1358.  
  1359.         AstSimpleName *lhs = compilation_unit -> ast_pool -> GenSimpleName(loc);
  1360.         VariableSymbol *variable = base_type -> FindThis(k);
  1361.         lhs -> symbol = (variable ? variable : base_type -> InsertThis(k));
  1362.  
  1363.         AstAssignmentExpression *assign = compilation_unit -> ast_pool
  1364.                                                            -> GenAssignmentExpression(AstAssignmentExpression::SIMPLE_EQUAL, loc);
  1365.         assign -> left_hand_side = lhs;
  1366.         assign -> expression     = rhs;
  1367.         assign -> symbol         = lhs -> Type();
  1368.  
  1369.         AstExpressionStatement *stmt  = compilation_unit -> ast_pool -> GenExpressionStatement();
  1370.         stmt -> expression            = assign;
  1371.         stmt -> semicolon_token_opt   = loc;
  1372.         stmt -> is_reachable          = true;
  1373.         stmt -> can_complete_normally = true;
  1374.  
  1375.         this_block -> AddStatement(stmt);
  1376.  
  1377.         if (lhs -> Type() -> IsSubclass(environment_type))
  1378.             break;
  1379.     }
  1380.  
  1381.     return base_type -> EnclosingInstance(k);
  1382. }
  1383.  
  1384.  
  1385. AstExpression *Semantic::CreateAccessToType(Ast *source, TypeSymbol *environment_type)
  1386. {
  1387.     TypeSymbol *this_type = ThisType();
  1388.  
  1389.     LexStream::TokenIndex tok;
  1390.  
  1391.     AstSimpleName *simple_name = source -> SimpleNameCast();
  1392.     AstFieldAccess *field_access = source -> FieldAccessCast();
  1393.     AstSuperCall *super_call = source -> SuperCallCast();
  1394.     AstThisCall *this_call = source -> ThisCallCast();
  1395.     AstClassInstanceCreationExpression *class_creation = source -> ClassInstanceCreationExpressionCast();
  1396.  
  1397.     if (simple_name)
  1398.          tok = simple_name -> identifier_token;
  1399.     else if (class_creation)
  1400.          tok = class_creation -> new_token;
  1401.     else if (super_call)
  1402.          tok = super_call -> super_token;
  1403.     else if (this_call)
  1404.          tok = this_call -> this_token;
  1405.     else if (field_access)
  1406.          tok = field_access -> dot_token;
  1407.     else assert(false);
  1408.  
  1409.     AstExpression *resolution;
  1410.  
  1411.     if (! this_type -> CanAccess(environment_type))
  1412.     {
  1413.         if (ExplicitConstructorInvocation())
  1414.             ReportSemError(SemanticError::ENCLOSING_INSTANCE_ACCESS_FROM_CONSTRUCTOR_INVOCATION,
  1415.                            (field_access ? field_access -> base -> LeftToken() : tok),
  1416.                            (field_access ? field_access -> base -> RightToken() : tok),
  1417.                            environment_type -> ContainingPackage() -> PackageName(),
  1418.                            environment_type -> ExternalName());
  1419.         else
  1420.         {
  1421.             SemanticEnvironment *env = state_stack.Top();
  1422.             for (; env; env = env -> previous) // check whether or not there is an intervening static type...
  1423.             {
  1424.                 if (env -> StaticRegion() || env -> Type() -> ACC_STATIC())
  1425.                     break;
  1426.             }
  1427.  
  1428.             if (env)
  1429.                  ReportSemError(SemanticError::ENCLOSING_INSTANCE_ACCESS_ACROSS_STATIC_REGION,
  1430.                                 (field_access ? field_access -> base -> LeftToken() : tok),
  1431.                                 (field_access ? field_access -> base -> RightToken() : tok),
  1432.                                 environment_type -> ContainingPackage() -> PackageName(),
  1433.                                 environment_type -> ExternalName(),
  1434.                                 env -> Type() -> ContainingPackage() -> PackageName(),
  1435.                                 env -> Type() -> ExternalName());
  1436.             else ReportSemError(SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE,
  1437.                                 (field_access ? field_access -> base -> LeftToken() : tok),
  1438.                                 (field_access ? field_access -> base -> RightToken() : tok),
  1439.                                 environment_type -> ContainingPackage() -> PackageName(),
  1440.                                 environment_type -> ExternalName());
  1441.         }
  1442.  
  1443.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1444.         resolution -> symbol = control.no_type;
  1445.     }
  1446.     else if (ExplicitConstructorInvocation())
  1447.     {
  1448.         VariableSymbol *variable = LocalSymbolTable().FindVariableSymbol(control.this0_name_symbol);
  1449.  
  1450.         assert(variable);
  1451.  
  1452.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1453.         resolution -> symbol = variable;
  1454.  
  1455.         //
  1456.         // TODO: Document this !!!
  1457.         //
  1458.         if (ExplicitConstructorInvocation() -> SuperCallCast())
  1459.         {
  1460.             TypeSymbol *containing_type = this_type -> ContainingType();
  1461.             if (! containing_type -> IsSubclass(environment_type))
  1462.             {
  1463.                 variable = FindInstance(containing_type, environment_type);
  1464.  
  1465.                 AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  1466.                 method_name -> base = resolution; // TODO: WARNING: sharing of Ast subtree !!!
  1467.                 method_name -> dot_token = tok;
  1468.                 method_name -> identifier_token = tok;
  1469.                 method_name -> symbol = variable;
  1470.  
  1471.                 AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1472.                 method_call -> method                  = method_name;
  1473.                 method_call -> left_parenthesis_token  = tok;
  1474.                 method_call -> right_parenthesis_token = tok;
  1475.  
  1476.                 assert(containing_type == variable -> owner -> TypeCast());
  1477.  
  1478.                 method_call -> symbol = containing_type -> GetReadAccessMethod(variable);
  1479.                 method_call -> AddArgument(resolution);
  1480.  
  1481.                 resolution = method_call;
  1482.             }
  1483.         }
  1484.     }
  1485.     else if (this_type -> IsSubclass(environment_type))
  1486.     {
  1487.         resolution = compilation_unit -> ast_pool -> GenThisExpression(tok);
  1488.         resolution -> symbol = this_type;
  1489.     }
  1490.     else
  1491.     {
  1492.         resolution = compilation_unit -> ast_pool -> GenSimpleName(tok);
  1493.         resolution -> symbol = FindInstance(this_type, environment_type);
  1494.     }
  1495.  
  1496.     return ((resolution -> symbol == control.no_type) || (resolution -> Type() == environment_type)
  1497.                                                        ? resolution
  1498.                                                        : ConvertToType(resolution, environment_type));
  1499. }
  1500.  
  1501.  
  1502. void Semantic::CreateAccessToScopedVariable(AstSimpleName *simple_name, TypeSymbol *environment_type)
  1503. {
  1504.     assert(environment_type -> IsOwner(ThisType()));
  1505.  
  1506.     VariableSymbol *variable_symbol = (VariableSymbol *) simple_name -> symbol;
  1507.  
  1508.     AstExpression *access_expression;
  1509.     if (variable_symbol -> ACC_STATIC())
  1510.     {
  1511.         access_expression = compilation_unit -> ast_pool -> GenSimpleName(simple_name -> identifier_token);
  1512.         access_expression -> symbol = environment_type;
  1513.     }
  1514.     else access_expression = CreateAccessToType(simple_name, environment_type);
  1515.  
  1516.     if (access_expression -> symbol != control.no_type)
  1517.     {
  1518.         assert(variable_symbol -> owner -> TypeCast());
  1519.  
  1520.         if (variable_symbol -> ACC_PRIVATE() ||
  1521.             (variable_symbol -> ACC_PROTECTED() &&
  1522.              variable_symbol -> owner -> TypeCast() -> ContainingPackage() != environment_type -> ContainingPackage()))
  1523.         {
  1524.             AstFieldAccess *method_name     = compilation_unit -> ast_pool -> GenFieldAccess();
  1525.             method_name -> base             = access_expression;
  1526.             method_name -> dot_token        = simple_name -> identifier_token;
  1527.             method_name -> identifier_token = simple_name -> identifier_token;
  1528.             method_name -> symbol           = variable_symbol;
  1529.  
  1530.             AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1531.             method_call -> method                  = method_name;
  1532.             method_call -> left_parenthesis_token  = simple_name -> identifier_token;
  1533.             method_call -> right_parenthesis_token = simple_name -> identifier_token;
  1534.             method_call -> symbol                  = environment_type -> GetReadAccessMethod(variable_symbol);
  1535.  
  1536.             if (! variable_symbol -> ACC_STATIC())
  1537.                 method_call -> AddArgument(access_expression); // TODO: WARNING: sharing of Ast subtree !!!
  1538.  
  1539.             simple_name -> resolution_opt = method_call;
  1540.         }
  1541.         else
  1542.         {
  1543.             AstFieldAccess *field_access     = compilation_unit -> ast_pool -> GenFieldAccess();
  1544.             field_access -> base             = access_expression;
  1545.             field_access -> dot_token        = simple_name -> identifier_token;
  1546.             field_access -> identifier_token = simple_name -> identifier_token;
  1547.             field_access -> symbol           = variable_symbol;
  1548.  
  1549.             simple_name -> resolution_opt = field_access;
  1550.         }
  1551.     }
  1552.  
  1553.     return;
  1554. }
  1555.  
  1556.  
  1557. void Semantic::CreateAccessToScopedMethod(AstMethodInvocation *method_call, TypeSymbol *environment_type)
  1558. {
  1559.     assert(environment_type -> IsOwner(ThisType()));
  1560.  
  1561.     MethodSymbol *method = (MethodSymbol *) method_call -> symbol;
  1562.     AstSimpleName *simple_name = (AstSimpleName *) method_call -> method;
  1563.  
  1564.     assert(simple_name -> SimpleNameCast());
  1565.  
  1566.     AstExpression *access_expression;
  1567.     if (method -> ACC_STATIC())
  1568.     {
  1569.         access_expression = compilation_unit -> ast_pool -> GenSimpleName(simple_name -> identifier_token);
  1570.         access_expression -> symbol = environment_type;
  1571.     }
  1572.     else access_expression = CreateAccessToType(simple_name, environment_type);
  1573.  
  1574.     if (access_expression -> symbol != control.no_type)
  1575.     {
  1576.         //
  1577.         // TODO: we have filed a query to Sun regarding the necessity of this check!
  1578.         //
  1579.         // SimpleNameAccessCheck(simple_name, method -> containing_type, method);
  1580.         //
  1581.         simple_name -> resolution_opt = access_expression;
  1582.         if (method -> ACC_PRIVATE() ||
  1583.             (method -> ACC_PROTECTED() &&
  1584.              method -> containing_type -> ContainingPackage() != environment_type -> ContainingPackage()))
  1585.         {
  1586.             if (method -> ACC_STATIC())
  1587.                 method_call -> symbol = environment_type -> GetReadAccessMethod(method);
  1588.             else
  1589.             {
  1590.                 AstMethodInvocation *old_method_call = method_call;
  1591.  
  1592.                 //
  1593.                 // TODO: WARNING: sharing of subtrees...
  1594.                 //
  1595.                 AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  1596.                 method_call -> method                  = old_method_call -> method;
  1597.                 method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  1598.                 method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  1599.                 method_call -> symbol                  = environment_type -> GetReadAccessMethod(method);
  1600.                 method_call -> AddArgument(access_expression);
  1601.                 for (int i = 0; i < old_method_call -> NumArguments(); i++)
  1602.                     method_call -> AddArgument(old_method_call -> Argument(i));
  1603.  
  1604.                 old_method_call -> symbol = method;
  1605.                 old_method_call -> resolution_opt = method_call;
  1606.             }
  1607.         }
  1608.     }
  1609.  
  1610.     return;
  1611. }
  1612.  
  1613.  
  1614. void Semantic::CheckSimpleName(AstSimpleName *simple_name, SemanticEnvironment *where_found)
  1615. {
  1616.     VariableSymbol *variable_symbol = simple_name -> symbol -> VariableCast();
  1617.  
  1618.     assert(variable_symbol);
  1619.  
  1620.     if (StaticRegion())
  1621.     {
  1622.         if (! (variable_symbol -> IsLocal() || variable_symbol -> ACC_STATIC()))
  1623.         {
  1624.             ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
  1625.                            simple_name -> identifier_token,
  1626.                            simple_name -> identifier_token,
  1627.                            lex_stream -> NameString(simple_name -> identifier_token));
  1628.         }
  1629.         else if (! variable_symbol -> IsDeclarationComplete())
  1630.         {
  1631.             ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
  1632.                            simple_name -> identifier_token,
  1633.                            simple_name -> identifier_token,
  1634.                            lex_stream -> NameString(simple_name -> identifier_token));
  1635.         }
  1636.     }
  1637.     else if (! variable_symbol -> ACC_STATIC()) // an instance variable?
  1638.     {
  1639.         TypeSymbol *containing_type = variable_symbol -> owner -> TypeCast(); // an instance field member ?
  1640.  
  1641.         if (containing_type) // variable must be a field
  1642.         {
  1643.             if (containing_type == ThisType() && (! variable_symbol -> IsDeclarationComplete())) // forward reference?
  1644.             {
  1645.                 ReportSemError(SemanticError::NAME_NOT_YET_AVAILABLE,
  1646.                                simple_name -> identifier_token,
  1647.                                simple_name -> identifier_token,
  1648.                                lex_stream -> NameString(simple_name -> identifier_token));
  1649.             }
  1650.             else if (ExplicitConstructorInvocation() && where_found == state_stack.Top())
  1651.             {
  1652.                 //
  1653.                 // If the variable in question is an instance variable that is
  1654.                 // declared in this_type (this_type is definitely a class) or
  1655.                 // one of its super classes, then we have an error:
  1656.                 //
  1657.                 ReportSemError(SemanticError::INSTANCE_VARIABLE_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  1658.                                simple_name -> identifier_token,
  1659.                                simple_name -> identifier_token,
  1660.                                lex_stream -> NameString(simple_name -> identifier_token),
  1661.                                containing_type -> Name());
  1662.             }
  1663.         }
  1664.     }
  1665.  
  1666.     return;
  1667. }
  1668.  
  1669.  
  1670. void Semantic::ProcessSimpleName(Ast *expr)
  1671. {
  1672.     TypeSymbol *this_type = ThisType();
  1673.  
  1674.     AstSimpleName *simple_name = (AstSimpleName *) expr;
  1675.     SemanticEnvironment *where_found;
  1676.     VariableSymbol *variable_symbol = FindVariableInEnvironment(where_found, state_stack.Top(), simple_name -> identifier_token);
  1677.     if (variable_symbol)
  1678.     {
  1679.         simple_name -> symbol = variable_symbol;
  1680.  
  1681.         assert(variable_symbol -> IsTyped());
  1682.  
  1683.         //
  1684.         // A variable_symbol FINAL must have an initial value.
  1685.         //
  1686.         if (variable_symbol -> ACC_FINAL())
  1687.         {
  1688.             if (variable_symbol -> IsDeclarationComplete())
  1689.                 simple_name -> value = variable_symbol -> initial_value;
  1690.             else if (variable_symbol -> declarator)
  1691.             {
  1692.                 AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  1693.                 //
  1694.                 // If the variable declarator in question exists and its computation is not
  1695.                 // pending (to avoid looping) and it has a simple expression initializer.
  1696.                 //
  1697.                 if (declarator &&
  1698.                     (! declarator -> pending) &&
  1699.                     declarator -> variable_initializer_opt &&
  1700.                     (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  1701.                 {
  1702.                     TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  1703.                     Semantic *sem = type -> semantic_environment -> sem;
  1704.                     simple_name -> value = sem -> ComputeFinalValue(declarator);
  1705.                 }
  1706.             }
  1707.         }
  1708.  
  1709.         CheckSimpleName(simple_name, where_found);
  1710.  
  1711.         //
  1712.         // If the variable belongs to an outer type, add the proper
  1713.         // pointer dereferences (and method access in the case of a
  1714.         // private variable) necessary to get to it.
  1715.         //
  1716.         if (where_found != state_stack.Top())
  1717.             CreateAccessToScopedVariable(simple_name, where_found -> Type());
  1718.     }
  1719.     else
  1720.     {
  1721.         //
  1722.         // We make a little effort to issue a better error message if we can identify
  1723.         // the name in question as the name of a method in the local type.
  1724.         //
  1725.         NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  1726.  
  1727.         MethodShadowSymbol *method_shadow;
  1728.         MethodSymbol *method;
  1729.  
  1730.         for (method_shadow = this_type -> expanded_method_table -> FindMethodShadowSymbol(name_symbol);
  1731.              method_shadow; method_shadow = method_shadow -> next_method)
  1732.         {
  1733.             method = method_shadow -> method_symbol;
  1734.  
  1735.             //
  1736.             // Make sure that method has been fully prepared
  1737.             //
  1738.             if (! method -> IsTyped())
  1739.                 method -> ProcessMethodSignature((Semantic *) this, simple_name -> identifier_token);
  1740.  
  1741.             if (method -> NumFormalParameters() == 0)
  1742.                 break;
  1743.         }
  1744.  
  1745.         if (method_shadow)
  1746.         {
  1747.              ReportSemError(SemanticError::METHOD_NOT_FIELD,
  1748.                             simple_name -> identifier_token,
  1749.                             simple_name -> identifier_token,
  1750.                             lex_stream -> NameString(simple_name -> identifier_token),
  1751.                             method -> containing_type -> ContainingPackage() -> PackageName(),
  1752.                             method -> containing_type -> ExternalName());
  1753.         }
  1754.         else if (FindType(simple_name -> identifier_token))
  1755.         {
  1756.              ReportSemError(SemanticError::TYPE_NOT_FIELD,
  1757.                             simple_name -> identifier_token,
  1758.                             simple_name -> identifier_token,
  1759.                             lex_stream -> NameString(simple_name -> identifier_token));
  1760.         }
  1761.         else
  1762.         {
  1763.             VariableSymbol *variable = FindMisspelledVariableName(this_type, simple_name -> identifier_token);
  1764.             if (variable)
  1765.                  ReportSemError(SemanticError::FIELD_NAME_MISSPELLED,
  1766.                                 simple_name -> identifier_token,
  1767.                                 simple_name -> identifier_token,
  1768.                                 name_symbol -> Name(),
  1769.                                 this_type -> ContainingPackage() -> PackageName(),
  1770.                                 this_type -> ExternalName(),
  1771.                                 variable -> Name());
  1772.             else ReportSemError(SemanticError::NAME_NOT_FOUND,
  1773.                                 simple_name -> identifier_token,
  1774.                                 simple_name -> identifier_token,
  1775.                                 lex_stream -> NameString(simple_name -> identifier_token));
  1776.         }
  1777.         simple_name -> symbol = control.no_type;
  1778.     }
  1779.  
  1780.     return;
  1781. }
  1782.  
  1783.  
  1784. void Semantic::TypeAccessCheck(Ast *ast, TypeSymbol *type)
  1785. {
  1786.     //
  1787.     // Unless we are processing the body of a type do not do type checking.
  1788.     // (This method may be invoked, for example, when FindFirstType invokes ProcessPackageOrType)
  1789.     //
  1790.     if (state_stack.Size() > 0)
  1791.     {
  1792.         TypeSymbol *this_type = ThisType();
  1793.  
  1794.         //
  1795.         // Type checking is necessary only for two types that are not enclosed within
  1796.         // the same outermost type. Note that if we are trying to access an inner type
  1797.         // T1.T2...Tn from this type, TypeAccessCheck is expected to be invoked first
  1798.         // for T1, then T1.T2, ... and finally for T1.T2...Tn, in turn. When invoked
  1799.         // for T1.T2, for example, this function only checks whether or not T1.T2
  1800.         // is accessible from "this" type. It does not recheck whether or not T1 is
  1801.         // accessible.
  1802.         //
  1803.         if (this_type -> outermost_type != type -> outermost_type)
  1804.         {
  1805.             if (type -> ACC_PRIVATE())
  1806.                  ReportTypeInaccessible(ast, type);
  1807.             else if (type -> ACC_PROTECTED())
  1808.             {
  1809.                 //
  1810.                 // TODO: we have filed a query to Sun regarding which test is required here!
  1811.                 //
  1812.                 // if (! (type -> ContainingPackage() == this_package || this_type -> IsSubclass(type)))
  1813.                 //
  1814.                 if (! (type -> ContainingPackage() == this_package || this_type -> HasProtectedAccessTo(type)))
  1815.                     ReportTypeInaccessible(ast, type);
  1816.             }
  1817.             else if (! (type -> ACC_PUBLIC() || type -> ContainingPackage() == this_package))
  1818.                  ReportTypeInaccessible(ast, type);
  1819.         }
  1820.     }
  1821.  
  1822.     return;
  1823. }
  1824.  
  1825.  
  1826. void Semantic::TypeNestAccessCheck(AstExpression *name)
  1827. {
  1828.     AstSimpleName *simple_name = name -> SimpleNameCast();
  1829.     AstFieldAccess *field_access = name -> FieldAccessCast();
  1830.  
  1831.     assert(simple_name || field_access);
  1832.  
  1833.     if (field_access)
  1834.         TypeNestAccessCheck(field_access -> base);
  1835.  
  1836.     TypeSymbol *type = (simple_name ? simple_name -> Type() : field_access -> Type());
  1837.     if (type)
  1838.         TypeAccessCheck(name, type);
  1839.  
  1840.     return;
  1841. }
  1842.  
  1843.  
  1844. void Semantic::ConstructorAccessCheck(AstClassInstanceCreationExpression *class_creation, MethodSymbol *constructor)
  1845. {
  1846.     TypeSymbol *this_type = ThisType();
  1847.     TypeSymbol *containing_type = constructor -> containing_type;
  1848.  
  1849.     if (this_type -> outermost_type != containing_type -> outermost_type)
  1850.     {
  1851.         if (constructor -> ACC_PRIVATE())
  1852.         {
  1853.             ReportSemError(SemanticError::PRIVATE_CONSTRUCTOR_NOT_ACCESSIBLE,
  1854.                            class_creation -> class_type -> LeftToken(),
  1855.                            class_creation -> right_parenthesis_token,
  1856.                            constructor -> Header(),
  1857.                            containing_type -> ContainingPackage() -> PackageName(),
  1858.                            containing_type -> ExternalName());
  1859.         }
  1860.         else if (! class_creation -> symbol -> TypeCast() -> Anonymous())
  1861.         {
  1862.             if (constructor -> ACC_PROTECTED())
  1863.             {
  1864.                 //
  1865.                 // TODO: we need to file a query to Sun regarding which test is required here!
  1866.                 // According to the rules 6.6.2 in the JLS, access to a protected constructor
  1867.                 // that is not contained in the same package is only valid through a "super(...)" call.
  1868.                 // However, javac seems to have relaxed this restriction to allow subclass access...
  1869.                 //
  1870.                 //
  1871.                 // TODO: we have filed a query to Sun regarding which test is required here!
  1872.                 //
  1873.                 // if (! (containing_type -> ContainingPackage() == this_package || this_type -> IsSubclass(containing_type)))
  1874.                 //
  1875.                 if (! (containing_type -> ContainingPackage() == this_package || this_type -> HasProtectedAccessTo(containing_type)))
  1876.                 {
  1877.                     ReportSemError(SemanticError::PROTECTED_CONSTRUCTOR_NOT_ACCESSIBLE,
  1878.                                    class_creation -> class_type -> LeftToken(),
  1879.                                    class_creation -> right_parenthesis_token,
  1880.                                    constructor -> Header(),
  1881.                                    containing_type -> ContainingPackage() -> PackageName(),
  1882.                                    containing_type -> ExternalName());
  1883.                 }
  1884.             }
  1885.             else if (! (constructor -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  1886.             {
  1887.                 ReportSemError(SemanticError::DEFAULT_CONSTRUCTOR_NOT_ACCESSIBLE,
  1888.                                class_creation -> class_type -> LeftToken(),
  1889.                                class_creation -> right_parenthesis_token,
  1890.                                constructor -> Header(),
  1891.                                containing_type -> ContainingPackage() -> PackageName(),
  1892.                                containing_type -> ExternalName());
  1893.             }
  1894.         }
  1895.     }
  1896.  
  1897.     return;
  1898. }
  1899.  
  1900.  
  1901. void Semantic::MemberAccessCheck(AstFieldAccess *field_access, TypeSymbol *base_type, Symbol *symbol)
  1902. {
  1903.     TypeSymbol *this_type = ThisType();
  1904.  
  1905.     VariableSymbol *variable_symbol = symbol -> VariableCast();
  1906.     MethodSymbol *method_symbol = symbol -> MethodCast();
  1907.  
  1908.     assert(variable_symbol || method_symbol);
  1909.  
  1910.     AccessFlags *flags = (variable_symbol ? (AccessFlags *) variable_symbol : (AccessFlags *) method_symbol);
  1911.     TypeSymbol *containing_type = (variable_symbol ? variable_symbol -> owner -> TypeCast() : method_symbol -> containing_type);
  1912.  
  1913.     assert(containing_type);
  1914.  
  1915.     AstExpression *base = field_access -> base;
  1916.  
  1917.     //
  1918.     // When this function, MemberAccessCheck is invoked, it is assumed that the function TypeAccessCheck
  1919.     // has already been invoked as follows:
  1920.     //
  1921.     //    TypeAccessCheck(base, base_type);
  1922.     //
  1923.     // and that the check below has already been performed.
  1924.     //
  1925.     //    if (! (base_type -> ACC_PUBLIC() || base_type -> ContainingPackage() == this_package))
  1926.     //        ReportTypeInaccessible(base, base_type);
  1927.     //
  1928.  
  1929.     if (this_type -> outermost_type != containing_type -> outermost_type)
  1930.     {
  1931.         if (flags -> ACC_PRIVATE())
  1932.         {
  1933.             ReportSemError((variable_symbol ? SemanticError::PRIVATE_FIELD_NOT_ACCESSIBLE
  1934.                                             : SemanticError::PRIVATE_METHOD_NOT_ACCESSIBLE),
  1935.                            field_access -> identifier_token,
  1936.                            field_access -> identifier_token,
  1937.                            lex_stream -> NameString(field_access -> identifier_token),
  1938.                            containing_type -> ContainingPackage() -> PackageName(),
  1939.                            containing_type -> ExternalName());
  1940.         }
  1941.         else if (flags -> ACC_PROTECTED())
  1942.         {
  1943.             //
  1944.             // TODO: This whole area is very Murky!!! This "feature" is not valid
  1945.             // according to the language spec. However, its use is so widespread
  1946.             // that we decided to accept it while issuing a strong "Caution" message
  1947.             // to encourage the user to not use it.
  1948.             //
  1949.             if (flags -> ACC_STATIC() &&
  1950.                 this_package != containing_type -> ContainingPackage() &&
  1951.                 this_type -> IsSubclass(containing_type))
  1952.             {
  1953.                 //
  1954.                 // Since "this" is a primary, it can be parenthesized. We remove all such parentheses here.
  1955.                 //
  1956.                 AstExpression *expr = base;
  1957.                 while(expr -> ParenthesizedExpressionCast())
  1958.                     expr = expr -> ParenthesizedExpressionCast() -> expression;
  1959.  
  1960.                 if (! (expr -> ThisExpressionCast() || expr -> SuperExpressionCast()))
  1961.                     ReportSemError((variable_symbol ? SemanticError::STATIC_PROTECTED_FIELD_ACCESS
  1962.                                                     : SemanticError::STATIC_PROTECTED_METHOD_ACCESS),
  1963.                                    field_access -> LeftToken(),
  1964.                                    field_access -> RightToken(),
  1965.                                    lex_stream -> NameString(field_access -> identifier_token),
  1966.                                    containing_type -> ContainingPackage() -> PackageName(),
  1967.                                    containing_type -> ExternalName());
  1968.             } else
  1969.  
  1970.             //
  1971.             // TODO: This whole area is very Murky!!! This is the only test that is required
  1972.             // according to the language spec and the inner classes document.
  1973.             //
  1974.             if (! (base -> IsSuperExpression() ||
  1975.                    containing_type -> ContainingPackage() == this_package ||
  1976.                    (this_type -> HasProtectedAccessTo(containing_type) &&
  1977.                     (base_type -> IsSubclass(this_type) || base_type -> IsOwner(this_type)))))
  1978.             {
  1979.                 ReportSemError((variable_symbol ? SemanticError::PROTECTED_FIELD_NOT_ACCESSIBLE
  1980.                                                 : SemanticError::PROTECTED_METHOD_NOT_ACCESSIBLE),
  1981.                                field_access -> identifier_token,
  1982.                                field_access -> identifier_token,
  1983.                                lex_stream -> NameString(field_access -> identifier_token),
  1984.                                containing_type -> ContainingPackage() -> PackageName(),
  1985.                                containing_type -> ExternalName());
  1986.             }
  1987.         }
  1988.         else if (! (flags -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  1989.         {
  1990.             ReportSemError((variable_symbol ? SemanticError::DEFAULT_FIELD_NOT_ACCESSIBLE
  1991.                                             : SemanticError::DEFAULT_METHOD_NOT_ACCESSIBLE),
  1992.                            field_access -> identifier_token,
  1993.                            field_access -> identifier_token,
  1994.                            lex_stream -> NameString(field_access -> identifier_token),
  1995.                            containing_type -> ContainingPackage() -> PackageName(),
  1996.                            containing_type -> ExternalName());
  1997.         }
  1998.     }
  1999.  
  2000.     return;
  2001. }
  2002.  
  2003.  
  2004. void Semantic::SimpleNameAccessCheck(AstSimpleName *simple_name, TypeSymbol *containing_type, Symbol *symbol)
  2005. {
  2006.     TypeSymbol *this_type = ThisType();
  2007.  
  2008.     VariableSymbol *variable_symbol = symbol -> VariableCast();
  2009.     MethodSymbol *method_symbol = symbol -> MethodCast();
  2010.  
  2011.     assert(variable_symbol || method_symbol);
  2012.  
  2013.     AccessFlags *flags = (variable_symbol ? (AccessFlags *) variable_symbol : (AccessFlags *) method_symbol);
  2014.  
  2015.     if (! (containing_type -> ACC_PUBLIC() || this_type -> ContainingPackage() == containing_type -> ContainingPackage()))
  2016.         ReportTypeInaccessible(simple_name, containing_type);
  2017.  
  2018.     if (this_type -> outermost_type != containing_type -> outermost_type)
  2019.     {
  2020.         if (flags -> ACC_PRIVATE())
  2021.         {
  2022.             ReportSemError((variable_symbol ? SemanticError::PRIVATE_FIELD_NOT_ACCESSIBLE
  2023.                                             : SemanticError::PRIVATE_METHOD_NOT_ACCESSIBLE),
  2024.                            simple_name -> identifier_token,
  2025.                            simple_name -> identifier_token,
  2026.                            lex_stream -> NameString(simple_name -> identifier_token),
  2027.                            containing_type -> ContainingPackage() -> PackageName(),
  2028.                            containing_type -> ExternalName());
  2029.         }
  2030.         else if (flags -> ACC_PROTECTED())
  2031.         {
  2032.             if (! (containing_type -> ContainingPackage() == this_package || this_type -> IsSubclass(containing_type)))
  2033.             {
  2034.                 ReportSemError((variable_symbol ? SemanticError::PROTECTED_FIELD_NOT_ACCESSIBLE
  2035.                                                 : SemanticError::PROTECTED_METHOD_NOT_ACCESSIBLE),
  2036.                                simple_name -> identifier_token,
  2037.                                simple_name -> identifier_token,
  2038.                                lex_stream -> NameString(simple_name -> identifier_token),
  2039.                                containing_type -> ContainingPackage() -> PackageName(),
  2040.                                containing_type -> ExternalName());
  2041.             }
  2042.         }
  2043.         else if (! (flags -> ACC_PUBLIC() || containing_type -> ContainingPackage() == this_package))
  2044.         {
  2045.             ReportSemError((variable_symbol ? SemanticError::DEFAULT_FIELD_NOT_ACCESSIBLE
  2046.                                             : SemanticError::DEFAULT_METHOD_NOT_ACCESSIBLE),
  2047.                            simple_name -> identifier_token,
  2048.                            simple_name -> identifier_token,
  2049.                            lex_stream -> NameString(simple_name -> identifier_token),
  2050.                            containing_type -> ContainingPackage() -> PackageName(),
  2051.                            containing_type -> ExternalName());
  2052.         }
  2053.     }
  2054.  
  2055.  
  2056.  
  2057.     return;
  2058. }
  2059.  
  2060.  
  2061. void Semantic::FindVariableMember(TypeSymbol *type, TypeSymbol *environment_type, AstFieldAccess *field_access)
  2062. {
  2063.     TypeSymbol *this_type = ThisType();
  2064.  
  2065.     //
  2066.     // TODO: Is this needed ?
  2067.     //
  2068.     // This operation may throw NullPointerException
  2069.     //
  2070.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  2071.     if (exception_set)
  2072.     {
  2073.         exception_set -> AddElement(control.RuntimeException());
  2074.     }
  2075.  
  2076.     if (type -> Bad())
  2077.     {
  2078.         //
  2079.         // If no error has been detected so far, report this as an error so that
  2080.         // we don't try to generate code later. On the other hand, if an error
  2081.         // had been detected prior to this, don't flood the user with spurious
  2082.         // messages.
  2083.         //
  2084.         if (NumErrors() == 0)
  2085.             ReportAccessedFieldNotFound(field_access, type);
  2086.         field_access -> symbol = control.no_type;
  2087.     }
  2088.     else if (type == control.null_type || type -> Primitive())
  2089.     {
  2090.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  2091.                        field_access -> base -> LeftToken(),
  2092.                        field_access -> base -> RightToken(),
  2093.                        type -> Name());
  2094.         field_access -> symbol = control.no_type;
  2095.     }
  2096.     else
  2097.     {
  2098.         TypeAccessCheck(field_access -> base, type);
  2099.  
  2100.         VariableSymbol *variable_symbol = FindVariableInType(type, field_access);
  2101.         if (variable_symbol)
  2102.         {
  2103.             assert(variable_symbol -> IsTyped());
  2104.  
  2105.             //
  2106.             // If a variable is FINAL and initialized with a constant expression,
  2107.             // we substitute the expression here. JLS section 15.27, pp 381-382.
  2108.             //
  2109.             // TODO: Note that the JLS is a bit ambiguous and that a strict reading
  2110.             // of 15.27 would prohibit substitution of a final constant value here.
  2111.             // However, javac seems to accept it and as the section on qualified name
  2112.             // only mentions "final" but not "static" and as it is not possible to derefence
  2113.             // a non-static final with a TypeName, we decided to relax the rules also.
  2114.             //
  2115.             if (variable_symbol -> ACC_FINAL() && field_access -> base -> IsName())
  2116.             {
  2117.                 //
  2118.                 // If the field declaration of the type has been completely processed,
  2119.                 // simply retrieve the value. Otherwise, compute the value of the
  2120.                 // initialization expression in question on the fly if the variable
  2121.                 // in question is not in the same type. Recall that static variables
  2122.                 // must be processed in the textual order in which they appear in the
  2123.                 // body of a type. Therefore, if the static initialization of a field
  2124.                 // refers to another variable in the same type it must have appeared
  2125.                 // before the current field declaration otherwise we will emit an error
  2126.                 // message later...
  2127.                 //
  2128.                 if (variable_symbol -> IsDeclarationComplete())
  2129.                     field_access -> value = variable_symbol -> initial_value;
  2130.                 else if (variable_symbol -> declarator)
  2131.                 {
  2132.                     AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  2133.                     //
  2134.                     // If the variable declarator in question exists and its computation is not
  2135.                     // pending (to avoid looping) and it has a simple expression initializer.
  2136.                     //
  2137.                     if (declarator && (! declarator -> pending) &&
  2138.                         declarator -> variable_initializer_opt &&
  2139.                         (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  2140.                     {
  2141.                         TypeSymbol *variable_type = (TypeSymbol *) variable_symbol -> owner;
  2142.                         Semantic *sem = variable_type -> semantic_environment -> sem;
  2143.                         field_access -> value = sem -> ComputeFinalValue(declarator);
  2144.                     }
  2145.                 }
  2146.             }
  2147.  
  2148.             TypeSymbol *containing_type = (TypeSymbol *) variable_symbol -> owner;
  2149.             //
  2150.             // Access to an private or protected variable in an enclosing type ?
  2151.             //
  2152.             if (this_type != containing_type &&
  2153.                 this_type -> outermost_type == environment_type -> outermost_type &&
  2154.                 (variable_symbol -> ACC_PRIVATE() ||
  2155.                  (variable_symbol -> ACC_PROTECTED() &&
  2156.                   containing_type -> ContainingPackage() != environment_type -> ContainingPackage())))
  2157.             {
  2158.                 assert((! variable_symbol -> ACC_PRIVATE()) || containing_type == environment_type);
  2159.  
  2160.                 if (field_access -> IsConstant())
  2161.                     field_access -> symbol = variable_symbol;
  2162.                 else
  2163.                 {
  2164.                     AstFieldAccess *method_name = compilation_unit -> ast_pool -> GenFieldAccess();
  2165.                     method_name -> base = field_access -> base; // TODO: WARNING: sharing of Ast subtree !!!
  2166.                     method_name -> dot_token = field_access -> identifier_token;
  2167.                     method_name -> identifier_token = field_access -> identifier_token;
  2168.                     method_name -> symbol = variable_symbol;
  2169.  
  2170.                     AstMethodInvocation *p       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2171.                     p -> method                  = method_name;
  2172.                     p -> left_parenthesis_token  = field_access -> identifier_token;
  2173.                     p -> right_parenthesis_token = field_access -> identifier_token;
  2174.                     p -> symbol                  = environment_type -> GetReadAccessMethod(variable_symbol);
  2175.  
  2176.                     if (! variable_symbol -> ACC_STATIC())
  2177.                         p -> AddArgument(field_access -> base);
  2178.  
  2179.                     field_access -> resolution_opt = p;
  2180.                     field_access -> symbol = p -> symbol;
  2181.                 }
  2182.             }
  2183.             else
  2184.             {
  2185.                 field_access -> symbol = variable_symbol;
  2186.                 MemberAccessCheck(field_access, type, variable_symbol);
  2187.             }
  2188.         }
  2189.         else
  2190.         {
  2191.             TypeSymbol *inner_type = FindNestedType(type, field_access -> identifier_token);
  2192.             if (inner_type)
  2193.                  ReportSemError(SemanticError::FIELD_IS_TYPE,
  2194.                                 field_access -> identifier_token,
  2195.                                 field_access -> identifier_token,
  2196.                                 lex_stream -> NameString(field_access -> identifier_token));
  2197.             else ReportAccessedFieldNotFound(field_access, type);
  2198.  
  2199.             field_access -> symbol = control.no_type;
  2200.         }
  2201.     }
  2202.  
  2203.     return;
  2204. }
  2205.  
  2206. //
  2207. // NOTE that method names are not processed here but by the function
  2208. // ProcessMethodName.
  2209. //
  2210. void Semantic::ProcessAmbiguousName(Ast *name)
  2211. {
  2212.     TypeSymbol *this_type = ThisType();
  2213.  
  2214.     AstSimpleName *simple_name;
  2215.  
  2216.     //
  2217.     // ...If the ambiguous name is a simple name,...
  2218.     //
  2219.     if ((simple_name = name -> SimpleNameCast()))
  2220.     {
  2221.         TypeSymbol *type;
  2222.         //
  2223.         // ... If the Identifier appears within the scope (6.3) if a local variable declaration (14.3)
  2224.         // or parameter declaration (8.4.1, 8.6.1, 14.18) with that name, then the ambiguous name is
  2225.         // reclassified as an ExpressionName...
  2226.         //
  2227.         // ...Otherwise, consider the class or interface C within whose declaration the Identifier occurs.
  2228.         // If C has one or more fields with that name, which may be either declared within it or inherited,
  2229.         // then the Ambiguous name is reclassified as an ExpressionName....
  2230.         //
  2231.         SemanticEnvironment *where_found;
  2232.         VariableSymbol *variable_symbol = FindVariableInEnvironment(where_found, state_stack.Top(), simple_name -> identifier_token);
  2233.         if (variable_symbol)
  2234.         {
  2235.             assert(variable_symbol -> IsTyped());
  2236.  
  2237.             //
  2238.             // A variable_symbol that is FINAL may have an initial value.
  2239.             // If variable_symbol is not final then its initial value is NULL.
  2240.             //
  2241.             simple_name -> value = variable_symbol -> initial_value;
  2242.             simple_name -> symbol = variable_symbol;
  2243.  
  2244.             CheckSimpleName(simple_name, where_found);
  2245.  
  2246.             //
  2247.             // If the variable belongs to an outer type, add the proper
  2248.             // pointer dereferences (and method access in the case of a
  2249.             // private variable) necessary to  get to it.
  2250.             //
  2251.             if (where_found != state_stack.Top())
  2252.                 CreateAccessToScopedVariable(simple_name, where_found -> Type());
  2253.         }
  2254.         //
  2255.         // ...Otherwise, if a type of that name is declared in the compilation unit (7.3) containing
  2256.         // the Identifier, either by a single-type-import declaration (7.5.1) or by a class or interface
  2257.         // type declaration (7.6), then the Ambiguous name is reclassified as a TypeName...
  2258.         //
  2259.         // ...Otherwise, if a type of that name is declared in another compilation unit (7.3) of the
  2260.         // package (7.1) of the compilation unit containing the Identifier, then the Ambiguous Name
  2261.         // is reclassified as a TypeName...
  2262.         //
  2263.         // ...Otherwise, if a type of that name is declared by exactly one type-import-on-demand declaration
  2264.         // (7.5.2) of the compilation unit containing the Identifier, then the AmbiguousName is reclassified
  2265.         // as a TypeName
  2266.         //
  2267.         // ...Otherwise, if a type of that name is declared by more than one type-import-on-demand declaration
  2268.         // of the compilation unit containing the Identifier, then a compile-time error results.
  2269.         //
  2270.         else if ((type = FindType(simple_name -> identifier_token)))
  2271.              simple_name -> symbol = type;
  2272.         //
  2273.         // ...Otherwise, the Ambiguous name is reclassified as a PackageName. A later step determines
  2274.         // whether or not a package of that name actually exists.
  2275.         //
  2276.         else
  2277.         {
  2278.             NameSymbol *name_symbol = lex_stream -> NameSymbol(simple_name -> identifier_token);
  2279.             PackageSymbol *package = control.external_table.FindPackageSymbol(name_symbol);
  2280.             if (package)
  2281.                 simple_name -> symbol = package;
  2282.             else
  2283.             {
  2284.                 package = control.external_table.InsertPackageSymbol(name_symbol, NULL);
  2285.                 control.FindPathsToDirectory(package);
  2286.                 simple_name -> symbol = package;
  2287.             }
  2288.         }
  2289.     }
  2290.     //
  2291.     // ...If the ambiguous name is a qualified name,...
  2292.     //
  2293.     else
  2294.     {
  2295.         AstFieldAccess *field_access = (AstFieldAccess *) name;
  2296.  
  2297.         assert(name -> FieldAccessCast());
  2298.  
  2299.         TypeSymbol *type = NULL;
  2300.  
  2301.         if (field_access -> IsClassAccess())
  2302.         {
  2303.             AddDependence(this_type, control.NoClassDefFoundError(), field_access -> identifier_token);
  2304.             AddDependence(this_type, control.ClassNotFoundException(), field_access -> identifier_token);
  2305.  
  2306.             AstTypeExpression *base = (AstTypeExpression *) field_access -> base;
  2307.  
  2308.             AstArrayType *array_type = base -> type -> ArrayTypeCast();
  2309.             if (array_type)
  2310.             {
  2311.                 AstPrimitiveType *primitive_type = array_type -> type -> PrimitiveTypeCast();
  2312.                 TypeSymbol *unit = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_type -> type));
  2313.                 type = unit -> GetArrayType((Semantic *) this, array_type -> NumBrackets());
  2314.             }
  2315.             else
  2316.             {
  2317.                 AstPrimitiveType *primitive_type = base -> type -> PrimitiveTypeCast();
  2318.                 type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(base -> type));
  2319.             }
  2320.  
  2321.             TypeSymbol *outermost_type = this_type -> outermost_type;
  2322.  
  2323.             if (type -> Primitive())
  2324.             {
  2325.                 if (type == control.int_type)
  2326.                      type = control.Integer();
  2327.                 else if (type == control.double_type)
  2328.                      type = control.Double();
  2329.                 else if (type == control.char_type)
  2330.                      type = control.Character();
  2331.                 else if (type == control.long_type)
  2332.                      type = control.Long();
  2333.                 else if (type == control.float_type)
  2334.                      type = control.Float();
  2335.                 else if (type == control.byte_type)
  2336.                      type = control.Byte();
  2337.                 else if (type == control.short_type)
  2338.                      type = control.Short();
  2339.                 else if (type == control.boolean_type)
  2340.                      type = control.Boolean();
  2341.                 else // (type == control.void_type)
  2342.                      type = control.Void();
  2343.                 base -> symbol = type;
  2344.  
  2345.                 VariableSymbol *variable_symbol = type -> FindVariableSymbol(control.type_name_symbol);
  2346.  
  2347.                 assert(variable_symbol);
  2348.  
  2349.                 if (control.option.deprecation &&
  2350.                     variable_symbol -> IsDeprecated() &&
  2351.                     variable_symbol -> owner -> TypeCast() -> outermost_type != ThisType() -> outermost_type)
  2352.                 {
  2353.                     ReportSemError(SemanticError::DEPRECATED_FIELD,
  2354.                                    field_access -> identifier_token,
  2355.                                    field_access -> identifier_token,
  2356.                                    variable_symbol -> Name(),
  2357.                                    variable_symbol -> owner -> TypeCast() -> ContainingPackage() -> PackageName(),
  2358.                                    variable_symbol -> owner -> TypeCast() -> ExternalName());
  2359.                 }
  2360.  
  2361.                 if (! variable_symbol -> IsTyped())
  2362.                     variable_symbol -> ProcessVariableSignature((Semantic *) this, field_access -> identifier_token);
  2363.  
  2364.                 field_access -> symbol = variable_symbol;
  2365.             }
  2366.             else
  2367.             {
  2368.                 TypeAccessCheck(base, array_type ? type -> base_type : type);
  2369.                 base -> symbol = type;
  2370.  
  2371.                 if (outermost_type -> ACC_INTERFACE())
  2372.                 {
  2373.                     TypeSymbol *class_literal_type = outermost_type -> FindOrInsertClassLiteralClass(field_access -> identifier_token);
  2374.                     AddDependence(this_type, class_literal_type, field_access -> identifier_token);
  2375.  
  2376.                     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2377.                     simple_name -> symbol = class_literal_type;
  2378.  
  2379.                     AstFieldAccess *method_access = compilation_unit -> ast_pool -> GenFieldAccess();
  2380.                     method_access -> base = simple_name;
  2381.                     method_access -> dot_token = field_access -> identifier_token;
  2382.                     method_access -> identifier_token = field_access -> identifier_token;
  2383.  
  2384.                     AstStringLiteral *string_literal = compilation_unit -> ast_pool -> GenStringLiteral(field_access -> identifier_token);
  2385.                     string_literal -> value = type -> FindOrInsertClassLiteralName(control);
  2386.                     string_literal -> symbol = control.String();
  2387.  
  2388.                     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2389.                     method_call -> method                  = method_access;
  2390.                     method_call -> left_parenthesis_token  = field_access -> identifier_token;
  2391.                     method_call -> AddArgument(string_literal);
  2392.                     method_call -> right_parenthesis_token = field_access -> identifier_token;
  2393.                     method_call -> symbol                  = class_literal_type -> ClassLiteralMethod();
  2394.  
  2395.                     field_access -> resolution_opt = method_call;
  2396.                     field_access -> symbol = (method_call -> symbol ? method_call -> symbol : control.no_type);
  2397.                 }
  2398.                 else
  2399.                 {
  2400.                     AddDependence(this_type, control.Class(), field_access -> identifier_token);
  2401.  
  2402.                     VariableSymbol *variable_symbol = outermost_type -> FindOrInsertClassLiteral(type);
  2403.                     if (this_type == outermost_type)
  2404.                     {
  2405.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2406.                         simple_name -> symbol = variable_symbol;
  2407.  
  2408.                         field_access -> symbol = variable_symbol;
  2409.                         field_access -> resolution_opt = simple_name;
  2410.                     }
  2411.                     else
  2412.                     {
  2413.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(field_access -> identifier_token);
  2414.                         simple_name -> symbol      = outermost_type;
  2415.  
  2416.                         AstFieldAccess *method_access     = compilation_unit -> ast_pool -> GenFieldAccess();
  2417.                         method_access -> base             = simple_name;
  2418.                         method_access -> dot_token        = field_access -> identifier_token;
  2419.                         method_access -> identifier_token = field_access -> identifier_token;
  2420.                         method_access -> symbol           = variable_symbol; // the variable in question
  2421.  
  2422.                         //
  2423.                         // Recall that the variable is static...
  2424.                         //
  2425.                         assert(variable_symbol -> ACC_STATIC());
  2426.  
  2427.                         AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2428.                         method_call -> method                  = method_access;
  2429.                         method_call -> left_parenthesis_token  = field_access -> identifier_token;
  2430.                         method_call -> right_parenthesis_token = field_access -> identifier_token;
  2431.                         method_call -> symbol                  = outermost_type -> GetWriteAccessMethod(variable_symbol);
  2432.  
  2433.                         field_access -> resolution_opt = method_call;
  2434.                         field_access -> symbol = outermost_type -> GetReadAccessMethod(variable_symbol);
  2435.                     }
  2436.                 }
  2437.             }
  2438.         }
  2439.         else
  2440.         {
  2441.             AstExpression* base = field_access -> base;
  2442.             AstFieldAccess *sub_field_access = base -> FieldAccessCast();
  2443.             simple_name = base -> SimpleNameCast();
  2444.  
  2445.             //
  2446.             // ...First, classify the name or expression to the left of the '.'...
  2447.             //
  2448.             if (simple_name || sub_field_access)
  2449.                  ProcessAmbiguousName(base);
  2450.             else ProcessExpression(base);
  2451.  
  2452.             if (base -> symbol == control.no_type)
  2453.             {
  2454.                 field_access -> symbol = control.no_type;
  2455.                 return;
  2456.             }
  2457.  
  2458.             wchar_t *identifier_name = lex_stream -> NameString(field_access -> identifier_token);
  2459.             PackageSymbol *package;
  2460.  
  2461.             Symbol *symbol = base -> symbol;
  2462.  
  2463.             if (field_access -> IsThisAccess() || field_access -> IsSuperAccess())
  2464.             {
  2465.                 TypeSymbol *enclosing_type = symbol -> TypeCast();
  2466.                 if (enclosing_type == control.no_type)
  2467.                     field_access -> symbol = control.no_type;
  2468.                 else
  2469.                 {
  2470.                     if (! enclosing_type)
  2471.                     {
  2472.                         ReportSemError(SemanticError::NOT_A_TYPE,
  2473.                                        field_access -> base -> LeftToken(),
  2474.                                        field_access -> base -> RightToken());
  2475.                         field_access -> symbol = control.no_type;
  2476.                     }
  2477.                     else if (enclosing_type -> ACC_INTERFACE())
  2478.                     {
  2479.                         ReportSemError(SemanticError::NOT_A_CLASS,
  2480.                                        field_access -> base -> LeftToken(),
  2481.                                        field_access -> base -> RightToken(),
  2482.                                        enclosing_type -> ContainingPackage() -> PackageName(),
  2483.                                        enclosing_type -> ExternalName());
  2484.                         field_access -> symbol = control.no_type;
  2485.                     }
  2486.                     else
  2487.                     {
  2488.                         if (! (this_type -> IsNestedIn(enclosing_type) && this_type -> CanAccess(enclosing_type)))
  2489.                         {
  2490.                             if (this_type == enclosing_type && field_access -> IsThisAccess())
  2491.                             {
  2492.                                 ReportSemError(SemanticError::MISPLACED_THIS_EXPRESSION,
  2493.                                                field_access -> LeftToken(),
  2494.                                                field_access -> RightToken());
  2495.                             }
  2496.                             else
  2497.                             {
  2498.                                 ReportSemError(SemanticError::ILLEGAL_THIS_FIELD_ACCESS,
  2499.                                                field_access -> LeftToken(),
  2500.                                                field_access -> RightToken(),
  2501.                                                enclosing_type -> ContainingPackage() -> PackageName(),
  2502.                                                enclosing_type -> ExternalName(),
  2503.                                                this_type -> ContainingPackage() -> PackageName(),
  2504.                                                this_type -> ExternalName());
  2505.                             }
  2506.  
  2507.                             field_access -> symbol = control.no_type;
  2508.                         }
  2509.                         else
  2510.                         {
  2511.                             //
  2512.                             // Note that in the case of a Super access, there will be further resolution later.
  2513.                             //
  2514.                             field_access -> resolution_opt = CreateAccessToType(field_access, enclosing_type);
  2515.                             field_access -> symbol = field_access -> resolution_opt -> symbol;
  2516.                         }
  2517.                     }
  2518.                 }
  2519.             }
  2520.             else if ((package = symbol -> PackageCast()))
  2521.             {
  2522.                 //
  2523.                 // ... If there is a package whose name is the name to the left of the '.' and that package
  2524.                 // contains a declaration of a type whose name is the same as the Identifier, then the
  2525.                 // AmbiguousName is reclassified as a TypeName...
  2526.                 //
  2527.                 NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  2528.                 type = package -> FindTypeSymbol(name_symbol);
  2529.  
  2530.                 if (type)
  2531.                 {
  2532.                     if (type -> SourcePending())
  2533.                         control.ProcessHeaders(type -> file_symbol);
  2534.                     field_access -> symbol = type;
  2535.                 }
  2536.                 else
  2537.                 {
  2538.                     FileSymbol *file_symbol = Control::GetFile(control, package, name_symbol);
  2539.                     if (file_symbol)
  2540.                     {
  2541.                         type = ReadType(file_symbol, package, name_symbol, field_access -> identifier_token);
  2542.                         field_access -> symbol = type;
  2543.                     }
  2544.                     //
  2545.                     // ... Otherwise, this AmbiguousName is reclassified as a PackageName. A later step determines
  2546.                     // whether or not a package of that name actually exists...
  2547.                     //
  2548.                     else
  2549.                     {
  2550.                         PackageSymbol *subpackage = package -> FindPackageSymbol(name_symbol);
  2551.                         if (! subpackage) // A new package ?
  2552.                         {
  2553.                             subpackage = package -> InsertPackageSymbol(name_symbol);
  2554.                             control.FindPathsToDirectory(subpackage);
  2555.                         }
  2556.                         field_access -> symbol = subpackage;
  2557.                     }
  2558.                 }
  2559.             }
  2560.             else if (sub_field_access && sub_field_access -> IsSuperAccess())
  2561.             {
  2562.                 if (sub_field_access -> Type() == control.no_type)
  2563.                     field_access -> symbol = control.no_type;
  2564.                 else if (sub_field_access -> Type() == control.Object())
  2565.                 {
  2566.                     ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
  2567.                                    sub_field_access -> LeftToken(),
  2568.                                    sub_field_access -> RightToken(),
  2569.                                    sub_field_access -> Type() -> ContainingPackage() -> PackageName(),
  2570.                                    sub_field_access -> Type() -> ExternalName());
  2571.                     field_access -> symbol = control.no_type;
  2572.                 }
  2573.                 else
  2574.                 {
  2575.                     type = sub_field_access -> Type() -> super;
  2576.                     FindVariableMember(type, sub_field_access -> Type(), field_access);
  2577.                 }
  2578.             }
  2579.             //
  2580.             // ...If the name to the left of the '.' is reclassified as a TypeName, then this AmbiguousName is
  2581.             // reclassified as an ExpressionName
  2582.             //
  2583.             else if ((type = symbol -> TypeCast()))
  2584.             {
  2585.                 if (type -> Bad())
  2586.                 {
  2587.                     //
  2588.                     // If no error has been detected so far, report this as an error so that
  2589.                     // we don't try to generate code later. On the other hand, if an error
  2590.                     // had been detected prior to this, don't flood the user with spurious
  2591.                     // messages.
  2592.                     //
  2593.                     if (NumErrors() == 0)
  2594.                         ReportAccessedFieldNotFound(field_access, type);
  2595.                     field_access -> symbol = control.no_type;
  2596.                 }
  2597.                 else if (type == control.null_type || type -> Primitive())
  2598.                 {
  2599.                     ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  2600.                                    base -> LeftToken(),
  2601.                                    base -> RightToken(),
  2602.                                    type -> Name());
  2603.                     field_access -> symbol = control.no_type;
  2604.                 }
  2605.                 else
  2606.                 {
  2607.                     TypeAccessCheck(base, type);
  2608.  
  2609.                     VariableSymbol *variable_symbol = FindVariableInType(type, field_access);
  2610.  
  2611.                     if (variable_symbol)
  2612.                     {
  2613.                         assert(variable_symbol -> IsTyped());
  2614.  
  2615.                         if (base -> IsName()) // a type name (as opposed to an expression) ?
  2616.                         {
  2617.                             if (variable_symbol -> ACC_STATIC())
  2618.                             {
  2619.                                 //
  2620.                                 // A variable_symbol that is STATIC and FINAL must have an initial value.
  2621.                                 // If it is dereferenced by a type name, then associate the value with the
  2622.                                 // subexpression to identify it as a constant subexpression.
  2623.                                 //
  2624.                                 if (variable_symbol -> ACC_FINAL())
  2625.                                 {
  2626.                                     //
  2627.                                     // If the field declaration of the type has been completely processed,
  2628.                                     // simply retrieve the value. Otherwise, compute the value of the
  2629.                                     // initialization expression in question on the fly if the variable
  2630.                                     // in question is not in the same type. Recall that static variables
  2631.                                     // must be processed in the textual order in which they appear in the
  2632.                                     // body of a type. Therefore, if the static initialization of a field
  2633.                                     // refers to another variable in the same type it must have appeared
  2634.                                     // before the current field declaration otherwise we will emit an error
  2635.                                     // message later...
  2636.                                     //
  2637.                                     if (variable_symbol -> IsDeclarationComplete())
  2638.                                         field_access -> value = variable_symbol -> initial_value;
  2639.                                     else if (variable_symbol -> declarator)
  2640.                                     {
  2641.                                         AstVariableDeclarator *declarator = variable_symbol -> declarator -> VariableDeclaratorCast();
  2642.                                         //
  2643.                                         // If the variable declarator in question exists and its computation is not
  2644.                                         // pending (to avoid looping) and it has a simple expression initializer.
  2645.                                         //
  2646.                                         if (declarator &&
  2647.                                             (! declarator -> pending) &&
  2648.                                             declarator -> variable_initializer_opt &&
  2649.                                             (! declarator -> variable_initializer_opt -> ArrayInitializerCast()))
  2650.                                         {
  2651.                                             TypeSymbol *type = (TypeSymbol *) variable_symbol -> owner;
  2652.                                             Semantic *sem = type -> semantic_environment -> sem;
  2653.                                             field_access -> value = sem -> ComputeFinalValue(declarator);
  2654.                                         }
  2655.                                     }
  2656.                                 }
  2657.                             }
  2658.                             else
  2659.                             {
  2660.                                 ReportSemError(SemanticError::NAME_NOT_CLASS_VARIABLE,
  2661.                                                field_access -> identifier_token,
  2662.                                                field_access -> identifier_token,
  2663.                                                identifier_name);
  2664.                             }
  2665.                         }
  2666.  
  2667.                         TypeSymbol *containing_type = (TypeSymbol *) variable_symbol -> owner;
  2668.                         //
  2669.                         // Access to a private or protected variable in an enclosing type ?
  2670.                         //
  2671.                         if (this_type != containing_type &&
  2672.                             this_type -> outermost_type == type -> outermost_type &&
  2673.                             (variable_symbol -> ACC_PRIVATE() ||
  2674.                              (variable_symbol -> ACC_PROTECTED() &&
  2675.                               containing_type -> ContainingPackage() != type -> ContainingPackage())))
  2676.                         {
  2677.                             assert((! variable_symbol -> ACC_PRIVATE()) || containing_type == type);
  2678.  
  2679.                             if (field_access -> IsConstant())
  2680.                                 field_access -> symbol = variable_symbol;
  2681.                             else
  2682.                             {
  2683.                                 AstFieldAccess *method_name     = compilation_unit -> ast_pool -> GenFieldAccess();
  2684.                                 method_name -> base             = field_access -> base;  // TODO: WARNING: sharing of Ast subtree !!!
  2685.                                 method_name -> dot_token        = field_access -> identifier_token;
  2686.                                 method_name -> identifier_token = field_access -> identifier_token;
  2687.                                 method_name -> symbol           = variable_symbol; // the variable in question
  2688.  
  2689.                                 //
  2690.                                 // variable_symbol is static.
  2691.                                 //
  2692.                                 AstMethodInvocation *p       = compilation_unit -> ast_pool -> GenMethodInvocation();
  2693.                                 p -> method                  = method_name;
  2694.                                 p -> left_parenthesis_token  = field_access -> identifier_token;
  2695.                                 p -> right_parenthesis_token = field_access -> identifier_token;
  2696.                                 p -> symbol                  = type -> GetReadAccessMethod(variable_symbol);
  2697.  
  2698.                                 if (! variable_symbol -> ACC_STATIC())
  2699.                                     p -> AddArgument(field_access -> base); // TODO: WARNING: sharing of Ast subtree !!!
  2700.  
  2701.                                 field_access -> resolution_opt = p;
  2702.                                 field_access -> symbol = p -> symbol;
  2703.                             }
  2704.                         }
  2705.                         else
  2706.                         {
  2707.                             field_access -> symbol = variable_symbol;
  2708.                             MemberAccessCheck(field_access, type, variable_symbol);
  2709.                         }
  2710.                     }
  2711.                     else
  2712.                     {
  2713.                         TypeSymbol *inner_type = FindNestedType(type, field_access -> identifier_token);
  2714.                         if (inner_type)
  2715.                         {
  2716.                             field_access -> symbol = inner_type;
  2717.                             TypeAccessCheck(field_access, inner_type);
  2718.                         }
  2719.                         else
  2720.                         {
  2721.                             ReportAccessedFieldNotFound(field_access, type);
  2722.                             field_access -> symbol = control.no_type;
  2723.                         }
  2724.                     }
  2725.                 }
  2726.             }
  2727.             //
  2728.             // ...If the name to the left of the '.' is reclassified as an ExpressionName, then this
  2729.             // AmbiguousName is reclassified as an instance member field reference. Note that we have two subcases to
  2730.             // consider: the case where the subexpression to the left is resolved to a variable and
  2731.             // the case where the subexpression is resolved to a method call.
  2732.             //
  2733.             else if (symbol -> VariableCast())
  2734.             {
  2735.                 assert(symbol -> VariableCast() -> IsTyped());
  2736.  
  2737.                 type = symbol -> VariableCast() -> Type();
  2738.  
  2739.                 FindVariableMember(type, type, field_access);
  2740.             }
  2741.             else if (symbol -> MethodCast())
  2742.             {
  2743.                 assert(symbol -> MethodCast() -> IsTyped());
  2744.  
  2745.                 type = symbol -> MethodCast() -> Type();
  2746.  
  2747.                 FindVariableMember(type, type, field_access);
  2748.             }
  2749.             else // illegal Name !!!
  2750.             {
  2751.                 ReportSemError(SemanticError::UNKNOWN_QUALIFIED_NAME_BASE,
  2752.                                base -> LeftToken(),
  2753.                                base -> RightToken(),
  2754.                                symbol -> Name());
  2755.                 field_access -> symbol = control.no_type;
  2756.             }
  2757.         }
  2758.  
  2759.         if (type)
  2760.         {
  2761.             TypeSymbol *parent_type = (type -> IsArray() ? type -> base_type : type);
  2762.             if (! parent_type -> Primitive())
  2763.                 AddDependence(this_type, parent_type, field_access -> identifier_token, field_access -> IsConstant());
  2764.         }
  2765.     }
  2766.  
  2767.     return;
  2768. }
  2769.  
  2770.  
  2771. void Semantic::ProcessFieldAccess(Ast *expr)
  2772. {
  2773.     AstFieldAccess *field_access = (AstFieldAccess *) expr;
  2774.  
  2775.     ProcessAmbiguousName(field_access);
  2776.  
  2777.         TypeSymbol *type;
  2778.  
  2779.     if (field_access -> symbol != control.no_type)
  2780.     {
  2781.         PackageSymbol *package = field_access -> symbol -> PackageCast();
  2782.         if (package)
  2783.         {
  2784.             ReportSemError(SemanticError::UNKNOWN_AMBIGUOUS_NAME,
  2785.                            field_access -> LeftToken(),
  2786.                            field_access -> RightToken(),
  2787.                            package -> PackageName());
  2788.             field_access -> symbol = control.no_type;
  2789.         }
  2790.         else if ((type = field_access -> symbol -> TypeCast()) && (! field_access -> IsThisAccess()))
  2791.         {
  2792.             ReportSemError(SemanticError::TYPE_NOT_FIELD,
  2793.                            field_access -> LeftToken(),
  2794.                            field_access -> RightToken(),
  2795.                            type -> Name());
  2796.             field_access -> symbol = control.no_type;
  2797.         }
  2798.         else // Assertion: either it's not a variable (an error) or the signature of the variable has been typed
  2799.         {
  2800.             assert((! field_access -> symbol -> VariableCast()) || field_access -> symbol -> VariableCast() -> IsTyped());
  2801.         }
  2802.     }
  2803.  
  2804.     return;
  2805. }
  2806.  
  2807.  
  2808. void Semantic::ProcessCharacterLiteral(Ast *expr)
  2809. {
  2810.     AstCharacterLiteral *char_literal = (AstCharacterLiteral *) expr;
  2811.  
  2812.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(char_literal -> character_literal_token);
  2813.  
  2814.     if (! literal -> value)
  2815.         control.int_pool.FindOrInsertChar(literal);
  2816.     if (literal -> value == control.BadValue())
  2817.     {
  2818.         ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  2819.                        char_literal -> LeftToken(),
  2820.                        char_literal -> RightToken());
  2821.         char_literal -> symbol = control.no_type;
  2822.     }
  2823.     else
  2824.     {
  2825.         char_literal -> value = literal -> value;
  2826.         char_literal -> symbol = control.char_type;
  2827.     }
  2828. }
  2829.  
  2830.  
  2831. void Semantic::ProcessIntegerLiteral(Ast *expr)
  2832. {
  2833.     AstIntegerLiteral *int_literal = (AstIntegerLiteral *) expr;
  2834.  
  2835.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(int_literal -> integer_literal_token);
  2836.  
  2837.     if (! literal -> value)
  2838.         control.int_pool.FindOrInsertInt(literal);
  2839.     if (literal -> value == control.BadValue())
  2840.     {
  2841.         ReportSemError(SemanticError::INVALID_INT_VALUE,
  2842.                        int_literal -> LeftToken(),
  2843.                        int_literal -> RightToken());
  2844.         int_literal -> symbol = control.no_type;
  2845.     }
  2846.     else
  2847.     {
  2848.         int_literal -> value = literal -> value;
  2849.         int_literal -> symbol = control.int_type;
  2850.     }
  2851. }
  2852.  
  2853.  
  2854. void Semantic::ProcessLongLiteral(Ast *expr)
  2855. {
  2856.     AstLongLiteral *long_literal = (AstLongLiteral *) expr;
  2857.  
  2858.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(long_literal -> long_literal_token);
  2859.  
  2860.     if (! literal -> value)
  2861.         control.long_pool.FindOrInsertLong(literal);
  2862.     if (literal -> value == control.BadValue())
  2863.     {
  2864.         ReportSemError(SemanticError::INVALID_LONG_VALUE,
  2865.                        long_literal -> LeftToken(),
  2866.                        long_literal -> RightToken());
  2867.         long_literal -> symbol = control.no_type;
  2868.     }
  2869.     else
  2870.     {
  2871.         long_literal -> value = literal -> value;
  2872.         long_literal -> symbol = control.long_type;
  2873.     }
  2874. }
  2875.  
  2876.  
  2877. void Semantic::ProcessFloatingPointLiteral(Ast *expr)
  2878. {
  2879.     AstFloatingPointLiteral *float_literal = (AstFloatingPointLiteral *) expr;
  2880.  
  2881.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(float_literal -> floating_point_literal_token);
  2882.  
  2883.     if (! literal -> value)
  2884.         control.float_pool.FindOrInsertFloat(literal);
  2885.     if (literal -> value == control.BadValue())
  2886.     {
  2887.         ReportSemError(SemanticError::INVALID_FLOAT_VALUE,
  2888.                        float_literal -> LeftToken(),
  2889.                        float_literal -> RightToken());
  2890.         float_literal -> symbol = control.no_type;
  2891.     }
  2892.     else
  2893.     {
  2894.         float_literal -> value = literal -> value;
  2895.         float_literal -> symbol = control.float_type;
  2896.     }
  2897. }
  2898.  
  2899.  
  2900. void Semantic::ProcessDoubleLiteral(Ast *expr)
  2901. {
  2902.     AstDoubleLiteral *double_literal = (AstDoubleLiteral *) expr;
  2903.  
  2904.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(double_literal -> double_literal_token);
  2905.  
  2906.     if (! literal -> value)
  2907.         control.double_pool.FindOrInsertDouble(literal);
  2908.     if (literal -> value == control.BadValue())
  2909.     {
  2910.         ReportSemError(SemanticError::INVALID_DOUBLE_VALUE,
  2911.                        double_literal -> LeftToken(),
  2912.                        double_literal -> RightToken());
  2913.         double_literal -> symbol = control.no_type;
  2914.     }
  2915.     else
  2916.     {
  2917.         double_literal -> value = literal -> value;
  2918.         double_literal -> symbol = control.double_type;
  2919.     }
  2920. }
  2921.  
  2922.  
  2923. void Semantic::ProcessTrueLiteral(Ast *expr)
  2924. {
  2925.     AstExpression *true_literal = (AstTrueLiteral *) expr;
  2926.  
  2927.     true_literal -> value = control.int_pool.FindOrInsert((int) 1);
  2928.     true_literal -> symbol = control.boolean_type;
  2929. }
  2930.  
  2931.  
  2932. void Semantic::ProcessFalseLiteral(Ast *expr)
  2933. {
  2934.     AstExpression *false_literal = (AstFalseLiteral *) expr;
  2935.  
  2936.     false_literal -> value = control.int_pool.FindOrInsert((int) 0);
  2937.     false_literal -> symbol = control.boolean_type;
  2938. }
  2939.  
  2940.  
  2941. void Semantic::ProcessStringLiteral(Ast *expr)
  2942. {
  2943.     AstStringLiteral *string_literal = (AstStringLiteral *) expr;
  2944.  
  2945.     LiteralSymbol *literal = lex_stream -> LiteralSymbol(string_literal -> string_literal_token);
  2946.  
  2947.     if (! literal -> value)
  2948.         control.Utf8_pool.FindOrInsertString(literal);
  2949.     if (literal -> value == control.BadValue())
  2950.     {
  2951.         ReportSemError(SemanticError::INVALID_STRING_VALUE,
  2952.                        string_literal -> LeftToken(),
  2953.                        string_literal -> RightToken());
  2954.         string_literal -> symbol = control.no_type;
  2955.     }
  2956.     else
  2957.     {
  2958.         string_literal -> value = literal -> value;
  2959.         string_literal -> symbol = control.String();
  2960.     }
  2961. }
  2962.  
  2963.  
  2964. void Semantic::ProcessArrayAccess(Ast *expr)
  2965. {
  2966.     AstArrayAccess *array_access = (AstArrayAccess *) expr;
  2967.  
  2968.     //
  2969.     // TODO: Is this needed ?
  2970.     //
  2971.     // This operation may throw NullPointerException or IndefOutOfBoundsException
  2972.     //
  2973.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  2974.     if (exception_set)
  2975.     {
  2976.         exception_set -> AddElement(control.RuntimeException());
  2977.     }
  2978.  
  2979.     ProcessExpression(array_access -> base);
  2980.     ProcessExpression(array_access -> expression);
  2981.     array_access -> expression = PromoteUnaryNumericExpression(array_access -> expression);
  2982.     if (array_access -> expression -> Type() != control.int_type && array_access -> expression -> symbol != control.no_type)
  2983.     {
  2984.         ReportSemError(SemanticError::TYPE_NOT_INTEGER,
  2985.                        array_access -> expression -> LeftToken(),
  2986.                        array_access -> expression -> RightToken(),
  2987.                        array_access -> expression -> Type() -> Name());
  2988.     }
  2989.  
  2990.     TypeSymbol *array_type = array_access -> base -> Type();
  2991.     if (array_type -> IsArray())
  2992.         array_access -> symbol = array_type -> ArraySubtype();
  2993.     else
  2994.     {
  2995.         if (array_type != control.no_type)
  2996.             ReportSemError(SemanticError::TYPE_NOT_ARRAY,
  2997.                            array_access -> base -> LeftToken(),
  2998.                            array_access -> base -> RightToken(),
  2999.                            array_access -> base -> Type() -> Name());
  3000.         array_access -> symbol = control.no_type;
  3001.     }
  3002.  
  3003.     return;
  3004. }
  3005.  
  3006.  
  3007. MethodSymbol *Semantic::FindMethodMember(TypeSymbol *type, TypeSymbol *environment_type, AstMethodInvocation *method_call)
  3008. {
  3009.     MethodSymbol *method = NULL;
  3010.     AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  3011.  
  3012.     if (type -> Bad())
  3013.     {
  3014.         //
  3015.         // If no error has been detected so far, report this as an error so that
  3016.         // we don't try to generate code later. On the other hand, if an error
  3017.         // had been detected prior to this, don't flood the user with spurious
  3018.         // messages.
  3019.         //
  3020.         if (NumErrors() == 0)
  3021.             ReportMethodNotFound(method_call, lex_stream -> NameString(field_access -> identifier_token));
  3022.         method_call -> symbol = control.no_type;
  3023.     }
  3024.     else if (type == control.null_type || type -> Primitive())
  3025.     {
  3026.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  3027.                        field_access -> base -> LeftToken(),
  3028.                        field_access -> base -> RightToken(),
  3029.                        type -> Name());
  3030.         method_call -> symbol = control.no_type;
  3031.     }
  3032.     else
  3033.     {
  3034.         TypeSymbol *this_type = ThisType();
  3035.         TypeAccessCheck(field_access -> base, type);
  3036.  
  3037.         method = FindMethodInType(type, method_call);
  3038.  
  3039.         if (method)
  3040.         {
  3041.             assert(method -> IsTyped());
  3042.  
  3043.             //
  3044.             // Access to an private or protected method in an enclosing type ?
  3045.             //
  3046.             if (this_type != method -> containing_type &&
  3047.                 this_type -> outermost_type == environment_type -> outermost_type &&
  3048.                 (method -> ACC_PRIVATE() ||
  3049.                  (method -> ACC_PROTECTED() &&
  3050.                   method -> containing_type -> ContainingPackage() != environment_type -> ContainingPackage())))
  3051.             {
  3052.                 assert((! method -> ACC_PRIVATE()) || method -> containing_type == environment_type);
  3053.  
  3054.                 if (method -> ACC_STATIC())
  3055.                     method_call -> symbol = environment_type -> GetReadAccessMethod(method);
  3056.                 else
  3057.                 {
  3058.                     AstMethodInvocation *old_method_call = method_call;
  3059.  
  3060.                     //
  3061.                     // TODO: WARNING: sharing of subtrees...
  3062.                     //
  3063.                     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3064.                     method_call -> method                  = old_method_call -> method;
  3065.                     method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  3066.                     method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  3067.                     method_call -> symbol                  = environment_type -> GetReadAccessMethod(method);
  3068.                     method_call -> AddArgument(field_access -> base);
  3069.                     for (int i = 0; i < old_method_call -> NumArguments(); i++)
  3070.                         method_call -> AddArgument(old_method_call -> Argument(i));
  3071.  
  3072.                     old_method_call -> symbol = method;
  3073.                     old_method_call -> resolution_opt = method_call;
  3074.                 }
  3075.             }
  3076.             else
  3077.             {
  3078.                 method_call -> symbol = method;
  3079.                 MemberAccessCheck(field_access, type, method);
  3080.             }
  3081.         }
  3082.         else method_call -> symbol = control.no_type;
  3083.     }
  3084.  
  3085.     return method;
  3086. }
  3087.  
  3088.  
  3089. void Semantic::ProcessMethodName(AstMethodInvocation *method_call)
  3090. {
  3091.     TypeSymbol *this_type = ThisType();
  3092.  
  3093.     //
  3094.     // TODO: Is this needed ?
  3095.     //
  3096.     // This operation may throw:
  3097.     //
  3098.     //        OutOfMemoryError
  3099.     //        NoSuchMethodError
  3100.     //        IllegalAccessError
  3101.     //        IncompatibleClassChangeError
  3102.     //
  3103.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  3104.     if (exception_set)
  3105.     {
  3106.         exception_set -> AddElement(control.Error());
  3107.     }
  3108.  
  3109.     AstSimpleName *simple_name;
  3110.     if ((simple_name = method_call -> method -> SimpleNameCast()))
  3111.     {
  3112.         SemanticEnvironment *where_found;
  3113.         MethodSymbol *method = FindMethodInEnvironment(where_found, state_stack.Top(), method_call);
  3114.  
  3115.         if (! method)
  3116.             method_call -> symbol = control.no_type;
  3117.         else
  3118.         {
  3119.             assert(method -> IsTyped());
  3120.  
  3121.             if (! method -> ACC_STATIC())
  3122.             {
  3123.                 //
  3124.                 // We are in a static region if we are:
  3125.                 //     . in the body of a static method
  3126.                 //     . in the body of a static initializer
  3127.                 //     . precessing an initializer expression for a static variable.
  3128.                 //
  3129.                 // See StaticRegion() Semantic.h for more detail.
  3130.                 //
  3131.                 // Note that a constructor is never static.
  3132.                 //
  3133.                 if (StaticRegion())
  3134.                 {
  3135.                     ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
  3136.                                    simple_name -> identifier_token,
  3137.                                    method_call -> right_parenthesis_token,
  3138.                                    lex_stream -> NameString(simple_name -> identifier_token));
  3139.                 }
  3140.                 else if (ExplicitConstructorInvocation())
  3141.                 {
  3142.                     if (this_type -> IsSubclass(method -> containing_type))
  3143.                     {
  3144.                         //
  3145.                         // If the method in question is an instance method
  3146.                         // that is declared in this_type (this_type is definitely
  3147.                         // a class) or one of its super classes, then we have an error ->
  3148.                         //
  3149.                         ReportSemError(SemanticError::INSTANCE_METHOD_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3150.                                        method_call -> LeftToken(),
  3151.                                        method_call -> RightToken(),
  3152.                                        method -> Header(),
  3153.                                        method -> containing_type -> Name());
  3154.                     }
  3155.                 }
  3156.             }
  3157.  
  3158.             method_call -> symbol = method;
  3159.  
  3160.             //
  3161.             // If the method is a private method belonging to an outer type,
  3162.             // give the ast simple_name access to its read_method.
  3163.             //
  3164.             if (where_found != state_stack.Top())
  3165.                 CreateAccessToScopedMethod(method_call, where_found -> Type());
  3166.         }
  3167.     }
  3168.     else
  3169.     {
  3170.         AstFieldAccess *field_access = method_call -> method -> FieldAccessCast();
  3171.         AstExpression* base = field_access -> base;
  3172.         AstFieldAccess *sub_field_access = base -> FieldAccessCast();
  3173.  
  3174.         if (base -> SimpleNameCast() || sub_field_access)
  3175.              ProcessAmbiguousName(base);
  3176.         else ProcessExpression(base);
  3177.  
  3178.         if (base -> symbol == control.no_type)
  3179.         {
  3180.             method_call -> symbol = control.no_type;
  3181.             return;
  3182.         }
  3183.  
  3184.         TypeSymbol *type = NULL;
  3185.         Symbol *symbol = base -> symbol;
  3186.  
  3187.         //
  3188.         // If the base is a "super" field access, resolve it before proceeding
  3189.         //
  3190.         if (sub_field_access && sub_field_access -> IsSuperAccess())
  3191.         {
  3192.             if (sub_field_access -> Type() == control.no_type)
  3193.                 method_call -> symbol = control.no_type;
  3194.             else if (sub_field_access -> Type() == control.Object())
  3195.             {
  3196.                 ReportSemError(SemanticError::OBJECT_HAS_NO_SUPER_TYPE,
  3197.                                sub_field_access -> LeftToken(),
  3198.                                sub_field_access -> RightToken(),
  3199.                                sub_field_access -> Type() -> ContainingPackage() -> PackageName(),
  3200.                                sub_field_access -> Type() -> ExternalName());
  3201.                 method_call -> symbol = control.no_type;
  3202.             }
  3203.             else
  3204.             {
  3205.                 MethodSymbol *method = FindMethodMember(sub_field_access -> Type() -> super, sub_field_access -> Type(), method_call);
  3206.                 field_access -> base = ConvertToType(sub_field_access, sub_field_access -> Type() -> super);
  3207.  
  3208.                 //
  3209.                 // TODO: This test was added in order to pass the test in section 8.4.3.1, page 159.
  3210.                 //       All I can find in the spec is that one example. Nowhere else could I find a
  3211.                 //       more formal statement.
  3212.                 //
  3213.                 if (method && method -> ACC_ABSTRACT())
  3214.                 {
  3215.                     ReportSemError(SemanticError::ABSTRACT_METHOD_INVOCATION,
  3216.                                    field_access -> LeftToken(),
  3217.                                    field_access -> identifier_token,
  3218.                                    lex_stream -> NameString(field_access -> identifier_token));
  3219.                 }
  3220.             }
  3221.         }
  3222.         else if ((type = symbol -> TypeCast()))
  3223.         {
  3224.             if (type -> Bad())
  3225.             {
  3226.                 //
  3227.                 // If no error has been detected so far, report this as an error so that
  3228.                 // we don't try to generate code later. On the other hand, if an error
  3229.                 // had been detected prior to this, don't flood the user with spurious
  3230.                 // messages.
  3231.                 //
  3232.                 if (NumErrors() == 0)
  3233.                     ReportMethodNotFound(method_call, lex_stream -> NameString(field_access -> identifier_token));
  3234.                 method_call -> symbol = control.no_type;
  3235.             }
  3236.             else if (type == control.null_type || type -> Primitive())
  3237.             {
  3238.                 ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  3239.                                base -> LeftToken(),
  3240.                                base -> RightToken(),
  3241.                                type -> Name());
  3242.                 method_call -> symbol = control.no_type;
  3243.             }
  3244.             else
  3245.             {
  3246.                 TypeAccessCheck(base, type);
  3247.  
  3248.                 MethodSymbol *method = FindMethodInType(type, method_call);
  3249.  
  3250.                 if (method)
  3251.                 {
  3252.                     assert(method -> IsTyped());
  3253.  
  3254.                     if (base -> IsName() && (! method -> ACC_STATIC()))
  3255.                     {
  3256.                         ReportSemError(SemanticError::METHOD_NOT_CLASS_METHOD,
  3257.                                        field_access -> LeftToken(),
  3258.                                        field_access -> identifier_token,
  3259.                                        lex_stream -> NameString(field_access -> identifier_token));
  3260.                     }
  3261.                     //
  3262.                     // TODO: This test was added in order to pass the test in section 8.4.3.1, page 159.
  3263.                     //       All I can find in the spec is that one example. Nowhere else could I find a
  3264.                     //       more formal statement.
  3265.                     //
  3266.                     else if (base -> IsSuperExpression() && method -> ACC_ABSTRACT())
  3267.                         ReportSemError(SemanticError::ABSTRACT_METHOD_INVOCATION,
  3268.                                        field_access -> LeftToken(),
  3269.                                        field_access -> identifier_token,
  3270.                                        lex_stream -> NameString(field_access -> identifier_token));
  3271.  
  3272.                     //
  3273.                     // Access to an private or protected method in an enclosing type ?
  3274.                     //
  3275.                     if ( this_type != method -> containing_type &&
  3276.                          this_type -> outermost_type == type -> outermost_type &&
  3277.                          (method -> ACC_PRIVATE() ||
  3278.                           (method -> ACC_PROTECTED() &&
  3279.                            type -> ContainingPackage() != method -> containing_type -> ContainingPackage())))
  3280.                     {
  3281.                         assert((! method -> ACC_PRIVATE()) || type == method -> containing_type);
  3282.  
  3283.                         if (method -> ACC_STATIC())
  3284.                             method_call -> symbol = type -> GetReadAccessMethod(method);
  3285.                         else
  3286.                         {
  3287.                             AstMethodInvocation *old_method_call = method_call;
  3288.  
  3289.                             //
  3290.                             // TODO: WARNING: sharing of subtrees...
  3291.                             //
  3292.                             AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3293.                             method_call -> method                  = old_method_call -> method;
  3294.                             method_call -> left_parenthesis_token  = old_method_call -> left_parenthesis_token;
  3295.                             method_call -> right_parenthesis_token = old_method_call -> right_parenthesis_token;
  3296.                             method_call -> symbol                  = type -> GetReadAccessMethod(method);
  3297.                             method_call -> AddArgument(field_access -> base);
  3298.                             for (int i = 0; i < old_method_call -> NumArguments(); i++)
  3299.                                 method_call -> AddArgument(old_method_call -> Argument(i));
  3300.  
  3301.                             old_method_call -> symbol = method;
  3302.                             old_method_call -> resolution_opt = method_call;
  3303.                         }
  3304.                     }
  3305.                     else
  3306.                     {
  3307.                         method_call -> symbol = method;
  3308.                         MemberAccessCheck(field_access, type, method);
  3309.                     }
  3310.                 }
  3311.                 else method_call -> symbol = control.no_type;
  3312.             }
  3313.         }
  3314.         //
  3315.         // ...If the name to the left of the '.' is reclassified as an ExpressionName, then this
  3316.         // method call is reclassified as an instance member method call. Note that we have two subcases to
  3317.         // consider: the case where the subexpression to the left is resolved to a variable and
  3318.         // the case where the subexpression is resolved to a method call.
  3319.         //
  3320.         else if (symbol -> VariableCast())
  3321.         {
  3322.             assert(symbol -> VariableCast() -> IsTyped());
  3323.  
  3324.             type = symbol -> VariableCast() -> Type();
  3325.  
  3326.             (void) FindMethodMember(type, type, method_call);
  3327.         }
  3328.         else if (symbol -> MethodCast())
  3329.         {
  3330.             assert(symbol -> MethodCast() -> IsTyped());
  3331.  
  3332.             type = symbol -> MethodCast() -> Type();
  3333.  
  3334.             (void) FindMethodMember(type, type, method_call);
  3335.         }
  3336.         else // illegal Name !!!
  3337.         {
  3338.             PackageSymbol *package = symbol -> PackageCast();
  3339.             NameSymbol *name_symbol = lex_stream -> NameSymbol(field_access -> identifier_token);
  3340.  
  3341.             if (package && (package -> FindTypeSymbol(name_symbol) || Control::GetFile(control, package, name_symbol)))
  3342.             {
  3343.                 ReportSemError(SemanticError::TYPE_NOT_METHOD,
  3344.                                field_access -> identifier_token,
  3345.                                field_access -> identifier_token,
  3346.                                name_symbol -> Name());
  3347.             }
  3348.             else
  3349.             {
  3350.                 ReportSemError(SemanticError::UNKNOWN_QUALIFIED_NAME_BASE,
  3351.                                field_access -> base -> LeftToken(),
  3352.                                field_access -> base -> RightToken(),
  3353.                                symbol -> Name());
  3354.             }
  3355.             method_call -> symbol = control.no_type;
  3356.         }
  3357.  
  3358.         if (type)
  3359.         {
  3360.             TypeSymbol *parent_type = (type -> IsArray() ? type -> base_type : type);
  3361.             if (! parent_type -> Primitive())
  3362.                 AddDependence(this_type, parent_type, field_access -> identifier_token);
  3363.         }
  3364.     }
  3365.  
  3366.     if (method_call -> symbol != control.no_type)
  3367.     {
  3368.         MethodSymbol *method = (MethodSymbol *) method_call -> symbol;
  3369.  
  3370.         if (exception_set)
  3371.         {
  3372.             for (int i = method -> NumThrows() - 1; i >= 0; i--)
  3373.                 exception_set -> AddElement(method -> Throws(i));
  3374.  
  3375.             //
  3376.             // This operation may throw:
  3377.             //
  3378.             //        NullPointerException
  3379.             //
  3380.             if (! method -> ACC_STATIC())
  3381.                 exception_set -> AddElement(control.RuntimeException());
  3382.  
  3383.             //
  3384.             // This operation may throw:
  3385.             //
  3386.             //        UnsatisfiedLinkError
  3387.             //
  3388.             // However Error was already added to the exception set for other possible causes. See Above
  3389.             //
  3390.             // if (method -> ACC_NATIVE())
  3391.             //    exception_set -> AddElement(control.Error());
  3392.             //
  3393.         }
  3394.  
  3395.         //
  3396.         // Recall that an instance initializer in the body of an anonymous type can
  3397.         // throw any exception. The test below allows us to skip such blocks.
  3398.         //
  3399.         if (! (this_type -> Anonymous() && ThisMethod() && ThisMethod() -> Identity() == control.block_init_name_symbol))
  3400.         {
  3401.             for (int k = method -> NumThrows() - 1; k >= 0; k--)
  3402.             {
  3403.                 TypeSymbol *exception = method -> Throws(k);
  3404.                 if (! CatchableException(exception))
  3405.                 {
  3406.                     ReportSemError(SemanticError::UNCATCHABLE_METHOD_THROWN_CHECKED_EXCEPTION,
  3407.                                    method_call -> LeftToken(),
  3408.                                    method_call -> RightToken(),
  3409.                                    method -> Header(),
  3410.                                    exception -> ContainingPackage() -> PackageName(),
  3411.                                    exception -> ExternalName());
  3412.                 }
  3413.             }
  3414.         }
  3415.  
  3416.         //
  3417.         // If the method was resolved to another method, process the resolved method.
  3418.         //
  3419.         method_call = (method_call -> resolution_opt ? (AstMethodInvocation *) method_call -> resolution_opt : method_call);
  3420.         method = (MethodSymbol *) method_call -> symbol;
  3421.  
  3422.         assert(method_call -> NumArguments() == method -> NumFormalParameters());
  3423.  
  3424.         for (int i = 0; i < method_call -> NumArguments(); i++)
  3425.         {
  3426.             AstExpression *expr = method_call -> Argument(i);
  3427.             if (expr -> Type() != method -> FormalParameter(i) -> Type())
  3428.                 method_call -> Argument(i) = ConvertToType(expr, method -> FormalParameter(i) -> Type());
  3429.         }
  3430.     }
  3431.  
  3432.     return;
  3433. }
  3434.  
  3435.  
  3436. void Semantic::ProcessMethodInvocation(Ast *expr)
  3437. {
  3438.     AstMethodInvocation *method_call = (AstMethodInvocation *) expr;
  3439.  
  3440.     bool no_bad_argument = true;
  3441.  
  3442.     for (int i = 0; i < method_call -> NumArguments(); i++)
  3443.     {
  3444.         AstExpression *expr = method_call -> Argument(i);
  3445.         ProcessExpressionOrStringConstant(expr);
  3446.         no_bad_argument = no_bad_argument && (expr -> symbol != control.no_type);
  3447.     }
  3448.  
  3449.     if (no_bad_argument)
  3450.          ProcessMethodName(method_call);
  3451.     else method_call -> symbol = control.no_type;
  3452.  
  3453.     assert(method_call -> symbol == control.no_type || ((MethodSymbol *) method_call -> symbol) -> IsTyped());
  3454.  
  3455.     return;
  3456. }
  3457.  
  3458.  
  3459. void Semantic::ProcessNullLiteral(Ast *expr)
  3460. {
  3461.     AstNullLiteral *null_literal = (AstNullLiteral *) expr;
  3462.     null_literal -> value = control.NullValue();
  3463.     null_literal -> symbol = control.null_type;
  3464.  
  3465.     return;
  3466. }
  3467.  
  3468.  
  3469. void Semantic::ProcessThisExpression(Ast *expr)
  3470. {
  3471.     AstThisExpression *this_expression = (AstThisExpression *) expr;
  3472.  
  3473.     if (StaticRegion())
  3474.     {
  3475.         ReportSemError(SemanticError::MISPLACED_THIS_EXPRESSION,
  3476.                        this_expression -> LeftToken(),
  3477.                        this_expression -> RightToken());
  3478.         this_expression -> symbol = control.no_type;
  3479.     }
  3480.     else if (ExplicitConstructorInvocation())
  3481.     {
  3482.         ReportSemError(SemanticError::THIS_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3483.                        this_expression -> LeftToken(),
  3484.                        this_expression -> RightToken(),
  3485.                        lex_stream -> NameString(this_expression -> this_token));
  3486.         this_expression -> symbol = control.no_type;
  3487.     }
  3488.     else this_expression -> symbol = ThisType();
  3489.  
  3490.     return;
  3491. }
  3492.  
  3493.  
  3494. void Semantic::ProcessSuperExpression(Ast *expr)
  3495. {
  3496.     AstSuperExpression *super_expression = (AstSuperExpression *) expr;
  3497.  
  3498.     if (StaticRegion() || ThisType() == control.Object())
  3499.     {
  3500.          ReportSemError(SemanticError::MISPLACED_SUPER_EXPRESSION,
  3501.                         super_expression -> LeftToken(),
  3502.                         super_expression -> RightToken());
  3503.          super_expression -> symbol = control.no_type;
  3504.     }
  3505.     else if (ExplicitConstructorInvocation())
  3506.     {
  3507.         ReportSemError(SemanticError::THIS_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3508.                        super_expression -> LeftToken(),
  3509.                        super_expression -> RightToken(),
  3510.                        lex_stream -> NameString(super_expression -> super_token));
  3511.          super_expression -> symbol = control.no_type;
  3512.     }
  3513.     else super_expression -> symbol = ThisType() -> super;
  3514. }
  3515.  
  3516.  
  3517. void Semantic::ProcessParenthesizedExpression(Ast *expr)
  3518. {
  3519.     AstParenthesizedExpression *parenthesized = (AstParenthesizedExpression *) expr;
  3520.  
  3521.     ProcessExpression(parenthesized -> expression);
  3522.     parenthesized -> value = parenthesized -> expression -> value;
  3523.     parenthesized -> symbol = parenthesized -> expression -> symbol;
  3524. }
  3525.  
  3526.  
  3527. void Semantic::UpdateGeneratedLocalConstructor(MethodSymbol *constructor)
  3528. {
  3529.     TypeSymbol *local_type = constructor -> containing_type;
  3530.     MethodSymbol *local_constructor = constructor -> LocalConstructor();
  3531.  
  3532.     assert(local_constructor -> IsGeneratedLocalConstructor());
  3533.  
  3534.     BlockSymbol *block_symbol = local_constructor -> block_symbol;
  3535.  
  3536.     for (int i = 0; i < constructor -> NumFormalParameters(); i++)
  3537.     {
  3538.         VariableSymbol *param = constructor -> FormalParameter(i),
  3539.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3540.  
  3541.         assert(symbol);
  3542.  
  3543.         symbol -> SetExternalIdentity(param -> ExternalIdentity()); // TODO: do we really need this ?
  3544.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3545.         if (control.IsDoubleWordType(symbol -> Type()))
  3546.             block_symbol -> max_variable_index++;
  3547.         local_constructor -> AddFormalParameter(symbol);
  3548.     }
  3549.  
  3550.     //
  3551.     // If we are dealing with a constructor generated for an anonymous type and
  3552.     // the super type of the anonymous type is an inner type then the generated
  3553.     // constructor accepts an additional formal parameter which is the containing
  3554.     // type of the super type, and the name of the parameter is #0.
  3555.     //
  3556.     VariableSymbol *super_this0_variable = block_symbol -> FindVariableSymbol(control.MakeParameter(0));
  3557.     if (super_this0_variable)
  3558.     {
  3559.         super_this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3560.         local_constructor -> AddFormalParameter(super_this0_variable);
  3561.     }
  3562.     local_constructor -> SetSignature(control);
  3563.  
  3564.     //
  3565.     //
  3566.     //
  3567.     AstConstructorDeclaration *constructor_declaration = (AstConstructorDeclaration *)
  3568.                                                          constructor -> method_or_constructor_declaration;
  3569.  
  3570.     assert(constructor_declaration -> ConstructorDeclarationCast());
  3571.  
  3572.     AstConstructorBlock *constructor_block = constructor_declaration -> constructor_body;
  3573.  
  3574.     if (! (constructor_block -> explicit_constructor_invocation_opt &&
  3575.            constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast()))
  3576.     {
  3577.         constructor_block -> AllocateLocalInitStatements(local_type -> NumConstructorParameters());
  3578.  
  3579.         //
  3580.         // Generate an assignment statement for each local variable parameter.
  3581.         // Note that we do not initialize the this$0 here as the real constructor
  3582.         // will do that. If the local_type is static, its constructor_parameters
  3583.         // list does not start with this$0.
  3584.         //
  3585.         for (int i = (local_type -> ACC_STATIC() ? 0 : 1); i < local_type -> NumConstructorParameters(); i++)
  3586.         {
  3587.             VariableSymbol *param = local_type -> ConstructorParameter(i),
  3588.                            *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3589.  
  3590.             assert(symbol);
  3591.  
  3592.             AstSimpleName *lhs = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3593.             lhs -> symbol = param;
  3594.  
  3595.             AstSimpleName *rhs = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3596.             rhs -> symbol = symbol;
  3597.  
  3598.             AstAssignmentExpression *assign = compilation_unit -> ast_pool
  3599.                                                                -> GenAssignmentExpression(AstAssignmentExpression::SIMPLE_EQUAL,
  3600.                                                                                           constructor_block -> left_brace_token);
  3601.             assign -> left_hand_side = lhs;
  3602.             assign -> expression     = rhs;
  3603.             assign -> symbol         = lhs -> Type();
  3604.  
  3605.             AstExpressionStatement *stmt = compilation_unit -> ast_pool -> GenExpressionStatement();
  3606.             stmt -> expression           = assign;
  3607.             stmt -> semicolon_token_opt  = constructor_block -> left_brace_token;
  3608.  
  3609.             stmt -> is_reachable = true;
  3610.             stmt -> can_complete_normally = true;
  3611.  
  3612.             constructor_block -> AddLocalInitStatement(stmt);
  3613.         }
  3614.     }
  3615.  
  3616.     //
  3617.     //
  3618.     //
  3619.     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3620.     simple_name -> symbol = constructor;
  3621.  
  3622.     assert(! constructor -> IsGeneratedLocalConstructor());
  3623.  
  3624.     AstMethodInvocation *method_call       = compilation_unit -> ast_pool -> GenMethodInvocation();
  3625.     method_call -> method                  = simple_name;
  3626.     method_call -> left_parenthesis_token  = constructor_block -> left_brace_token;
  3627.     method_call -> right_parenthesis_token = constructor_block -> left_brace_token;
  3628.     method_call -> symbol                  = simple_name -> symbol;
  3629.  
  3630.     method_call -> AllocateArguments(constructor -> NumFormalParameters() + 1);
  3631.     if (! local_type -> ACC_STATIC())
  3632.     {
  3633.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3634.         simple_name -> symbol = block_symbol -> FindVariableSymbol(control.this0_name_symbol);
  3635.  
  3636.         assert(simple_name -> symbol);
  3637.  
  3638.         method_call -> AddArgument(simple_name);
  3639.     }
  3640.  
  3641.     for (int k = 0; k < constructor -> NumFormalParameters(); k++)
  3642.     {
  3643.         VariableSymbol *param = constructor -> FormalParameter(k),
  3644.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  3645.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(constructor_block -> left_brace_token);
  3646.         simple_name -> symbol = symbol;
  3647.         method_call -> AddArgument(simple_name);
  3648.     }
  3649.  
  3650.     AstExpressionStatement *stmt  = compilation_unit -> ast_pool -> GenExpressionStatement();
  3651.     stmt -> expression            = method_call;
  3652.     stmt -> semicolon_token_opt   = constructor_block -> left_brace_token;
  3653.  
  3654.     stmt -> is_reachable          = true;
  3655.     stmt -> can_complete_normally = true;
  3656.  
  3657.     constructor_block -> original_constructor_invocation = stmt;
  3658.  
  3659.     return;
  3660. }
  3661.  
  3662.  
  3663. void Semantic::UpdateLocalConstructors(TypeSymbol *inner_type)
  3664. {
  3665.     if (! ThisType() -> IsLocal()) // the method containing inner_type is not itself embedded in another method
  3666.     {
  3667.         //
  3668.         // Compute the set of local_classes we need to process here - they are
  3669.         // the inner_type itself and all the classes that are embedded in its body.
  3670.         //
  3671.         Tuple<TypeSymbol *> local_classes(8);
  3672.  
  3673.         TypeSymbol *outermost_type = inner_type -> outermost_type;
  3674.         if (outermost_type -> local) // The set of local types in the outermost type is not empty?
  3675.         {
  3676.             for (TypeSymbol *local_type = (TypeSymbol *) outermost_type -> local -> FirstElement();
  3677.                              local_type;
  3678.                              local_type = (TypeSymbol *) outermost_type -> local -> NextElement())
  3679.             {
  3680.                 if (local_type -> CanAccess(inner_type))
  3681.                     local_classes.Next() = local_type;
  3682.             }
  3683.         }
  3684.  
  3685.         for (int j = 0; j < outermost_type -> num_anonymous_types(); j++)
  3686.         {
  3687.             if (outermost_type -> AnonymousType(j) -> CanAccess(inner_type))
  3688.                 local_classes.Next() = outermost_type -> AnonymousType(j);
  3689.         }
  3690.  
  3691.         //
  3692.         // We now update each type T2 containing a call to a constructor of
  3693.         // T1 to make sure that T2 has a copy of or access to all the local
  3694.         // variables required by T1.
  3695.         //
  3696.         for (int k = 0; k < local_classes.Length(); k++)
  3697.         {
  3698.             TypeSymbol *target_local_type = local_classes[k];
  3699.  
  3700.             for (int i = 0; i < target_local_type -> NumLocalConstructorCallEnvironments(); i++)
  3701.             {
  3702.                 SemanticEnvironment *env = target_local_type -> LocalConstructorCallEnvironment(i);
  3703.                 TypeSymbol *source_local_type = env -> Type();
  3704.                 if (! source_local_type -> CanAccess(target_local_type))
  3705.                 {
  3706.                     for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3707.                          j < target_local_type -> NumConstructorParameters(); j++)
  3708.                     {
  3709.                         VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local;
  3710.  
  3711.                         //
  3712.                         // If there does not exist a variable with the same identity as the local or
  3713.                         // there exists such a variable but it is not the local then make a copy of
  3714.                         // the local in the source type.
  3715.                         //
  3716.                         if (env -> symbol_table.FindVariableSymbol(local -> Identity()) != local)
  3717.                             (void) source_local_type -> FindOrInsertLocalShadow(local);
  3718.                     }
  3719.                 }
  3720.             }
  3721.         }
  3722.  
  3723.         //
  3724.         // Now update the constructor bodies to reflect the new local variable counts and mark the local_type completed.
  3725.         //
  3726.         for (int l = 0; l < local_classes.Length(); l++)
  3727.         {
  3728.             TypeSymbol *local_type = local_classes[l];
  3729.  
  3730.             AstClassDeclaration *class_declaration = local_type -> declaration -> ClassDeclarationCast();
  3731.             AstClassInstanceCreationExpression *class_creation = local_type -> declaration -> ClassInstanceCreationExpressionCast();
  3732.  
  3733.             assert(class_declaration || class_creation);
  3734.  
  3735.             AstClassBody *class_body = (class_declaration ? class_declaration -> class_body : class_creation -> class_body_opt);
  3736.  
  3737.             if (class_body -> default_constructor)
  3738.                  UpdateGeneratedLocalConstructor(class_body -> default_constructor -> constructor_symbol);
  3739.             else
  3740.             {
  3741.                 for (int i = 0; i < class_body -> NumConstructors(); i++)
  3742.                     UpdateGeneratedLocalConstructor(class_body -> Constructor(i) -> constructor_symbol);
  3743.  
  3744.                 for (int k = 0; k < local_type -> NumPrivateAccessConstructors(); k++)
  3745.                     UpdateGeneratedLocalConstructor(local_type -> PrivateAccessConstructor(k));
  3746.             }
  3747.  
  3748.             local_type -> MarkLocalClassProcessingCompleted();
  3749.         }
  3750.  
  3751.         //
  3752.         // Now update the constructor calls
  3753.         //
  3754.         for (int m = 0; m < local_classes.Length(); m++)
  3755.         {
  3756.             TypeSymbol *target_local_type = local_classes[m];
  3757.  
  3758.             assert(target_local_type -> LocalClassProcessingCompleted());
  3759.  
  3760.             for (int i = 0; i < target_local_type -> NumLocalConstructorCallEnvironments(); i++)
  3761.             {
  3762.                 Ast *call = target_local_type -> LocalConstructorCallEnvironment(i) -> ast_construct;
  3763.                 SemanticEnvironment *env = target_local_type -> LocalConstructorCallEnvironment(i);
  3764.                 TypeSymbol *source_local_type = env -> Type();
  3765.  
  3766.                 AstClassInstanceCreationExpression *class_creation;
  3767.                 AstSuperCall *super_call;
  3768.                 AstThisCall *this_call;
  3769.  
  3770.                 if ((class_creation = call -> ClassInstanceCreationExpressionCast()))
  3771.                 {
  3772.                     if (class_creation -> symbol != control.no_type)
  3773.                     {
  3774.                         if (source_local_type -> CanAccess(target_local_type))
  3775.                         {
  3776.                             for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3777.                                  j < target_local_type -> NumConstructorParameters(); j++)
  3778.                             {
  3779.                                 AstSimpleName *simple_name = compilation_unit -> ast_pool
  3780.                                                                               -> GenSimpleName(class_creation -> new_token);
  3781.                                 VariableSymbol *variable_symbol = target_local_type -> ConstructorParameter(j);
  3782.                                 simple_name -> symbol = variable_symbol;
  3783.                                 if (source_local_type != target_local_type)
  3784.                                 {
  3785.                                     simple_name -> symbol = variable_symbol -> accessed_local;
  3786.  
  3787.                                     state_stack.Push(source_local_type -> semantic_environment);
  3788.                                     CreateAccessToScopedVariable(simple_name, target_local_type);
  3789.                                     state_stack.Pop();
  3790.                                 }
  3791.                                 class_creation -> AddLocalArgument(simple_name);
  3792.                             }
  3793.                         }
  3794.                         else
  3795.                         {
  3796.                             for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3797.                                  j < target_local_type -> NumConstructorParameters(); j++)
  3798.                             {
  3799.                                 VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local;
  3800.  
  3801.                                 AstSimpleName *simple_name = compilation_unit -> ast_pool
  3802.                                                                               -> GenSimpleName(class_creation -> new_token);
  3803.                                 //
  3804.                                 // If there does not exist a variable with the same identity as the local or
  3805.                                 // there exists such a variable but it is not the local then make a copy of
  3806.                                 // the local in the source type.
  3807.                                 //
  3808.                                 simple_name -> symbol = (env -> symbol_table.FindVariableSymbol(local -> Identity()) == local
  3809.                                                               ? local
  3810.                                                               : source_local_type -> FindOrInsertLocalShadow(local));
  3811.  
  3812.                                 assert(simple_name -> symbol -> VariableCast());
  3813.  
  3814.                                 class_creation -> AddLocalArgument(simple_name);
  3815.                             }
  3816.                         }
  3817.  
  3818.                         MethodSymbol *constructor = (MethodSymbol *) class_creation -> class_type -> symbol;
  3819.  
  3820.                         assert(constructor);
  3821.                         assert(constructor -> MethodCast());
  3822.                         assert(! constructor -> IsGeneratedLocalConstructor());
  3823.                         assert(constructor -> LocalConstructor());
  3824.  
  3825.                         class_creation -> class_type -> symbol = constructor -> LocalConstructor();
  3826.                     }
  3827.                 }
  3828.                 else if ((super_call = call -> SuperCallCast()))
  3829.                 {
  3830.                     if (super_call -> symbol -> MethodCast())
  3831.                     {
  3832.                         for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3833.                              j < target_local_type -> NumConstructorParameters(); j++)
  3834.                         {
  3835.                             VariableSymbol *local = target_local_type -> ConstructorParameter(j) -> accessed_local,
  3836.                                            *local_shadow = source_local_type -> FindOrInsertLocalShadow(local);
  3837.  
  3838.                             AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(super_call -> super_token);
  3839.                             simple_name -> symbol = env -> symbol_table.FindVariableSymbol(local_shadow -> Identity());
  3840.  
  3841.                             assert(simple_name -> symbol -> VariableCast());
  3842.  
  3843.                             super_call -> AddLocalArgument(simple_name);
  3844.                         }
  3845.  
  3846.                         MethodSymbol *constructor = (MethodSymbol *) super_call -> symbol;
  3847.  
  3848.                         assert(constructor -> MethodCast() && (! constructor -> IsGeneratedLocalConstructor()));
  3849.                         assert(constructor -> LocalConstructor());
  3850.  
  3851.                         super_call -> symbol = constructor -> LocalConstructor();
  3852.                     }
  3853.                 }
  3854.                 else
  3855.                 {
  3856.                     this_call = (AstThisCall *) call;
  3857.  
  3858.                     assert(this_call -> ThisCallCast());
  3859.  
  3860.                     if (this_call -> symbol -> MethodCast())
  3861.                     {
  3862.                         for (int j = (target_local_type -> ACC_STATIC() ? 0 : 1);
  3863.                              j < target_local_type -> NumConstructorParameters(); j++)
  3864.                         {
  3865.                             VariableSymbol *local = target_local_type -> ConstructorParameter(j);
  3866.  
  3867.                             AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(this_call -> this_token);
  3868.                             simple_name -> symbol = env -> symbol_table.FindVariableSymbol(local -> Identity());
  3869.  
  3870.                             assert(simple_name -> symbol -> VariableCast());
  3871.  
  3872.                             this_call -> AddLocalArgument(simple_name);
  3873.                         }
  3874.  
  3875.                         MethodSymbol *constructor = (MethodSymbol *) this_call -> symbol;
  3876.  
  3877.                         assert(constructor -> MethodCast() && (! constructor -> IsGeneratedLocalConstructor()));
  3878.                         assert(constructor -> LocalConstructor());
  3879.  
  3880.                         this_call -> symbol = constructor -> LocalConstructor();
  3881.                     }
  3882.                 }
  3883.             }
  3884.         }
  3885.     }
  3886.  
  3887.     return;
  3888. }
  3889.  
  3890.  
  3891. void Semantic::GetAnonymousConstructor(AstClassInstanceCreationExpression *class_creation, TypeSymbol *anonymous_type)
  3892. {
  3893.     LexStream::TokenIndex left_loc  = class_creation -> class_type -> LeftToken(),
  3894.                           right_loc = class_creation -> right_parenthesis_token;
  3895.  
  3896.     TypeSymbol *super_type = anonymous_type -> super;
  3897.     MethodSymbol *super_constructor = FindConstructor(super_type, class_creation, left_loc, right_loc);
  3898.     if (! super_constructor)
  3899.     {
  3900.         class_creation -> class_type -> symbol = control.no_type;
  3901.         return;
  3902.     }
  3903.  
  3904.     assert(super_constructor -> IsTyped());
  3905.  
  3906.     //
  3907.     // Make constructor symbol. The associated symbol table will not contain too many elements...
  3908.     //
  3909.     BlockSymbol *block_symbol = new BlockSymbol(super_constructor -> NumFormalParameters() + 3);
  3910.     block_symbol -> max_variable_index = 1; // All types need a spot for "this".
  3911.  
  3912.     MethodSymbol *constructor = anonymous_type -> InsertConstructorSymbol(control.init_name_symbol);
  3913.     constructor -> SetType(control.void_type);
  3914.     constructor -> SetContainingType(anonymous_type);
  3915.     constructor -> SetBlockSymbol(block_symbol);
  3916.     constructor -> SetACC_PUBLIC();
  3917.  
  3918.     //
  3919.     // Report error is super constructor has throws clause, but add the exceptions to the local throws
  3920.     // clause to avoid spurious errors later !!!
  3921.     //
  3922.     int num_throws = super_constructor -> NumThrows();
  3923.     if (num_throws > 0)
  3924.     {
  3925.         for (int i = 0; i < num_throws; i++)
  3926.         {
  3927.             TypeSymbol *exception = super_constructor -> Throws(i);
  3928.             ReportSemError(SemanticError::CONSTRUCTOR_DOES_NOT_THROW_SUPER_EXCEPTION,
  3929.                           class_creation -> new_token,
  3930.                           class_creation -> RightToken(),
  3931.                           StringConstant::US_EMPTY,
  3932.                           exception -> ContainingPackage() -> PackageName(),
  3933.                           exception -> ExternalName(),
  3934.                           super_constructor -> containing_type -> ContainingPackage() -> PackageName(),
  3935.                           super_constructor -> containing_type -> ExternalName());
  3936.  
  3937.             constructor -> AddThrows(exception);
  3938.         }
  3939.     }
  3940.  
  3941.     VariableSymbol *this0_variable = NULL;
  3942.     if (anonymous_type -> IsInner())
  3943.     {
  3944.         this0_variable = block_symbol -> InsertVariableSymbol(control.this0_name_symbol);
  3945.         this0_variable -> MarkSynthetic();
  3946.         this0_variable -> SetType(anonymous_type -> ContainingType());
  3947.         this0_variable -> SetOwner(constructor);
  3948.         this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3949.     }
  3950.  
  3951.     for (int j = 0; j < super_constructor -> NumFormalParameters(); j++)
  3952.     {
  3953.         VariableSymbol *param = super_constructor -> FormalParameter(j),
  3954.                        *symbol = block_symbol -> InsertVariableSymbol(param -> Identity());
  3955.         symbol -> SetType(param -> Type());
  3956.         symbol -> SetOwner(constructor);
  3957.         symbol -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  3958.         if (control.IsDoubleWordType(symbol -> Type()))
  3959.             block_symbol -> max_variable_index++;
  3960.         constructor -> AddFormalParameter(symbol);
  3961.     }
  3962.  
  3963.     //
  3964.     //
  3965.     //
  3966.     AstSuperCall *super_call              = compilation_unit -> ast_pool -> GenSuperCall();
  3967.     super_call -> base_opt                = class_creation -> base_opt; // save initial base_opt
  3968.     super_call -> dot_token_opt           = class_creation -> new_token;
  3969.     super_call -> super_token             = class_creation -> new_token;
  3970.     super_call -> left_parenthesis_token  = class_creation -> new_token;
  3971.     super_call -> right_parenthesis_token = class_creation -> new_token;
  3972.     super_call -> semicolon_token         = class_creation -> new_token;
  3973.  
  3974.     super_call -> is_reachable            = true;
  3975.     super_call -> can_complete_normally   = true;
  3976.     super_call -> symbol                  = super_constructor;
  3977.  
  3978.     //
  3979.     // If we are in a static region, the anonymous constructor does not need a this$0 argument.
  3980.     // Otherwise, a this$0 argument that points to an instance of the immediately enclosing
  3981.     // class is required.
  3982.     //
  3983.     if (anonymous_type -> ACC_STATIC())
  3984.         class_creation -> base_opt = NULL;
  3985.     else
  3986.     {
  3987.         //
  3988.         // Within an explicit constructor invocation, a class that is immediately nested
  3989.         // in the class being created is not accessible.
  3990.         //
  3991.         if (ExplicitConstructorInvocation() && anonymous_type -> ContainingType() == ThisType())
  3992.         {
  3993.             ReportSemError(SemanticError::INNER_CONSTRUCTOR_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  3994.                            class_creation -> LeftToken(),
  3995.                            class_creation -> RightToken(),
  3996.                            anonymous_type -> ContainingPackage() -> PackageName(),
  3997.                            anonymous_type -> ExternalName(),
  3998.                            ThisType() -> ContainingPackage() -> PackageName(),
  3999.                            ThisType() -> ExternalName());
  4000.             class_creation -> base_opt = NULL;
  4001.         }
  4002.         else class_creation -> base_opt = CreateAccessToType(class_creation, anonymous_type -> ContainingType());
  4003.     }
  4004.  
  4005.     AstClassBody *class_body = class_creation -> class_body_opt;
  4006.  
  4007.     AstReturnStatement *return_statement = compilation_unit -> ast_pool -> GenReturnStatement();
  4008.     return_statement -> return_token     = class_body -> left_brace_token;
  4009.     return_statement -> expression_opt   = NULL;
  4010.     return_statement -> semicolon_token  = class_body -> left_brace_token;
  4011.     return_statement -> is_reachable     = true;
  4012.  
  4013.     AstBlock *block                = compilation_unit -> ast_pool -> GenBlock();
  4014.     block -> block_symbol          = constructor -> block_symbol -> InsertBlockSymbol(1); // this symbol table will be empty
  4015.     block -> left_brace_token      = class_body -> left_brace_token;
  4016.     block -> right_brace_token     = class_body -> left_brace_token;
  4017.  
  4018.     block -> is_reachable          = true;
  4019.     block -> can_complete_normally = false;
  4020.     block -> AllocateBlockStatements(1); // this block contains one statement
  4021.     block -> AddStatement(return_statement);
  4022.  
  4023.     AstConstructorBlock *constructor_block                   = compilation_unit -> ast_pool -> GenConstructorBlock();
  4024.     constructor_block -> left_brace_token                    = class_body -> left_brace_token;
  4025.     constructor_block -> explicit_constructor_invocation_opt = super_call;
  4026.     constructor_block -> block                               = block;
  4027.     constructor_block -> right_brace_token                   = class_body -> left_brace_token;
  4028.  
  4029.     AstMethodDeclarator *method_declarator       = compilation_unit -> ast_pool -> GenMethodDeclarator();
  4030.     method_declarator -> identifier_token        = left_loc;
  4031.     method_declarator -> left_parenthesis_token  = class_creation -> left_parenthesis_token;
  4032.     method_declarator -> right_parenthesis_token = right_loc;
  4033.  
  4034.     AstConstructorDeclaration *constructor_declaration  = compilation_unit -> ast_pool -> GenConstructorDeclaration();
  4035.     constructor_declaration -> constructor_declarator   = method_declarator;
  4036.     constructor_declaration -> constructor_body         = constructor_block;
  4037.  
  4038.     constructor_declaration -> constructor_symbol = constructor;
  4039.     constructor -> method_or_constructor_declaration = constructor_declaration;
  4040.  
  4041.     //
  4042.     // Note that the constructor for the anonymous type is not added to the class body here
  4043.     // beacause we've already completely compiled it and the arguments to its super call
  4044.     // do not contain "valid" SimpleName Ast expressions. It is added to the constructor
  4045.     // body later in get_anonymous_type...
  4046.     //
  4047.     // class_body -> default_constructor = constructor_declaration;
  4048.     //
  4049.     VariableSymbol *super_this0_variable = NULL;
  4050.  
  4051.     if (anonymous_type -> IsLocal())
  4052.     {
  4053.         GenerateLocalConstructor(constructor);
  4054.  
  4055.         MethodSymbol *generated_constructor = constructor -> LocalConstructor();
  4056.  
  4057.         assert(! constructor -> IsGeneratedLocalConstructor());
  4058.         assert(generated_constructor);
  4059.  
  4060.         block_symbol = generated_constructor -> block_symbol; // use the environment of the generated constructor...
  4061.  
  4062.         if (super_call -> base_opt)
  4063.         {
  4064.             //
  4065.             // Add the this$0 parameter for the super type. However, only mark it complete and
  4066.             // do not yet assign a number to it. This will be done after we know
  4067.             // how many extra "local" variable shadows are needed. See UpdateGeneratedLocalConstructor
  4068.             //
  4069.             super_this0_variable = block_symbol -> InsertVariableSymbol(control.MakeParameter(0));
  4070.             super_this0_variable -> MarkSynthetic();
  4071.             super_this0_variable -> SetType(super_call -> base_opt -> Type());
  4072.             super_this0_variable -> SetOwner(generated_constructor);
  4073.             super_this0_variable -> MarkComplete();
  4074.         }
  4075.  
  4076.         if (super_type -> IsLocal()) // a local type may use enclosed local variables?
  4077.         {
  4078.             if (super_type -> LocalClassProcessingCompleted())
  4079.             {
  4080.                 assert(super_constructor -> LocalConstructor() && (! super_constructor -> IsGeneratedLocalConstructor()));
  4081.  
  4082.                 super_call -> symbol = super_constructor -> LocalConstructor();
  4083.  
  4084.                 //
  4085.                 // TODO: Should we set the size for the super_call arguments here ???
  4086.                 //
  4087.                 for (int i = (super_type -> ACC_STATIC() ? 0 : 1); i < super_type -> NumConstructorParameters(); i++)
  4088.                 {
  4089.                     VariableSymbol *local = super_type -> ConstructorParameter(i) -> accessed_local,
  4090.                                    *local_shadow = anonymous_type -> FindOrInsertLocalShadow(local);
  4091.  
  4092.                     AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(super_call -> super_token);
  4093.                     simple_name -> symbol = block_symbol -> FindVariableSymbol(local_shadow -> Identity());
  4094.  
  4095.                     assert(simple_name -> symbol);
  4096.  
  4097.                     super_call -> AddLocalArgument(simple_name);
  4098.                 }
  4099.             }
  4100.             else // are we currently within the body of the type in question ?
  4101.                 super_type -> AddLocalConstructorCallEnvironment(GetEnvironment(super_call));
  4102.         }
  4103.     }
  4104.     else if (super_call -> base_opt)
  4105.     {
  4106.         super_this0_variable = block_symbol -> InsertVariableSymbol(control.MakeParameter(0));
  4107.         super_this0_variable -> MarkSynthetic();
  4108.         super_this0_variable -> SetType(super_call -> base_opt -> Type());
  4109.         super_this0_variable -> SetOwner(constructor);
  4110.         super_this0_variable -> SetLocalVariableIndex(block_symbol -> max_variable_index++);
  4111.  
  4112.         constructor -> AddFormalParameter(super_this0_variable);
  4113.     }
  4114.  
  4115.     //
  4116.     // Complete the definition of the constructor and update the super call accordingly.
  4117.     //
  4118.     if (super_this0_variable)
  4119.     {
  4120.         class_creation -> AddArgument(super_call -> base_opt); // pass the original base expression as argument to anonymous class.
  4121.  
  4122.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4123.         simple_name -> symbol = super_this0_variable;
  4124.         super_call -> base_opt = simple_name; // pass the base expression argument to the super class
  4125.     }
  4126.  
  4127.     constructor -> SetSignature(control, this0_variable); // we now have all the information to set the signature of the constructor.
  4128.  
  4129.     //
  4130.     // Are we guaranteed to have all the info available here? Yes,
  4131.     // because if the anonymous type is not local to a method, then its super
  4132.     // type cannot be local to a method. Therefore, no extra argument (other than
  4133.     // the proper this$0 specified in the base) is needed. If on the other hand the
  4134.     // anonymous type is local and its supertype is also local, it must have appeared
  4135.     // before the anonymous type and therefore its information has already been computed.
  4136.     //
  4137.     for (int k = 0; k < super_constructor -> NumFormalParameters(); k++)
  4138.     {
  4139.         VariableSymbol *param = super_constructor -> FormalParameter(k),
  4140.                        *symbol = block_symbol -> FindVariableSymbol(param -> Identity());
  4141.  
  4142.         assert(symbol);
  4143.  
  4144.         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4145.         simple_name -> symbol = symbol;
  4146.         super_call -> AddArgument(simple_name);
  4147.     }
  4148.  
  4149.     class_creation -> class_type -> symbol = constructor;
  4150.  
  4151.     return;
  4152. }
  4153.  
  4154.  
  4155. TypeSymbol *Semantic::GetAnonymousType(AstClassInstanceCreationExpression *class_creation, TypeSymbol *super_type)
  4156. {
  4157.     TypeSymbol *this_type = ThisType();
  4158.  
  4159.     if (super_type -> ACC_FINAL())
  4160.     {
  4161.          ReportSemError(SemanticError::SUPER_IS_FINAL,
  4162.                         class_creation -> class_type -> LeftToken(),
  4163.                         class_creation -> class_type -> RightToken(),
  4164.                         super_type -> ContainingPackage() -> PackageName(),
  4165.                         super_type -> ExternalName());
  4166.     }
  4167.  
  4168.     AstClassBody *class_body = class_creation -> class_body_opt;
  4169.     TypeSymbol *outermost_type = this_type -> outermost_type;
  4170.  
  4171.     //
  4172.     // Make up a proper name for the anonymous type
  4173.     //
  4174.     IntToWstring value(outermost_type -> num_anonymous_types() + 1);
  4175.  
  4176.     int length = value.Length() + outermost_type -> NameLength() + 1; // +1 for $
  4177.     wchar_t *anonymous_name = new wchar_t[length + 1];
  4178.     wcscpy(anonymous_name, outermost_type -> Name());
  4179.     wcscat(anonymous_name, StringConstant::US__DS);
  4180.     wcscat(anonymous_name, value.String());
  4181.  
  4182.     NameSymbol *name_symbol = control.FindOrInsertName(anonymous_name, length);
  4183.  
  4184.     assert((! ThisMethod()) || LocalSymbolTable().Top());
  4185.  
  4186.     TypeSymbol *inner_type = (ThisMethod() ? LocalSymbolTable().Top() -> InsertAnonymousTypeSymbol(name_symbol)
  4187.                                            : this_type -> InsertAnonymousTypeSymbol(name_symbol));
  4188.     inner_type -> SetACC_PRIVATE();
  4189.     inner_type -> MarkAnonymous();
  4190.     inner_type -> outermost_type = outermost_type;
  4191.     inner_type -> supertypes_closure = new SymbolSet;
  4192.     inner_type -> subtypes_closure = new SymbolSet;
  4193.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this, inner_type, state_stack.Top());
  4194.     inner_type -> declaration = class_creation;
  4195.     inner_type -> file_symbol = source_file_symbol;
  4196.     inner_type -> SetOwner(ThisMethod() ? (Symbol *) ThisMethod() : (Symbol *) this_type);
  4197.     //
  4198.     // Add 3 extra elements for padding. May need a default constructor and other support elements.
  4199.     //
  4200.     inner_type -> SetSymbolTable(class_body -> NumClassBodyDeclarations() + 3);
  4201.     inner_type -> SetLocation();
  4202.     inner_type -> SetSignature(control);
  4203.  
  4204.     //
  4205.     // TODO: As an anonymous type cannot be a super class, it makes sense to mark
  4206.     // is final. This allows jikes to be consistent with javac in emitting an
  4207.     // error message when the anonymous class is checked in an instanceof
  4208.     // operation against an interface. However, this fact is not documented
  4209.     // in the 1.1 document. Furthermore, the class file that is emitted for an
  4210.     // anonymous flag (when processed by javac) does not have the FINAL flag turned on.
  4211.     // We also turn this flag off after processing the body of the anonymmous type.
  4212.     // See bolow...
  4213.     //
  4214.     inner_type -> SetACC_FINAL();
  4215.  
  4216.     //
  4217.     // Note that if the anonymous type we are constructing was encountered while
  4218.     // we were processing an explicit constructor invocation, we assume we are
  4219.     // in a static region. This allows the anonymous type to be constructed without
  4220.     // requiring a this$0 parameter as the "this" pointer argument that would
  4221.     // be passed such a this$0 parameter does not yet exist at that point. Furthermore,
  4222.     // making the anonymous type static also prevents it from accessing any surrounding
  4223.     // instance variable that would require the this$0 pointer.
  4224.     //
  4225.     if (StaticRegion() || (ExplicitConstructorInvocation() && inner_type -> ContainingType() == ThisType()))
  4226.          inner_type -> SetACC_STATIC();
  4227.     else inner_type -> InsertThis(0);
  4228.  
  4229.     if (super_type -> ACC_INTERFACE())
  4230.     {
  4231.          inner_type -> AddInterface(super_type);
  4232.          inner_type -> super = control.Object();
  4233.     }
  4234.     else inner_type -> super = super_type;
  4235.  
  4236.     outermost_type -> AddAnonymousType(inner_type);
  4237.     delete [] anonymous_name;
  4238.  
  4239.     //
  4240.     //
  4241.     //
  4242.     GetAnonymousConstructor(class_creation, inner_type);
  4243.  
  4244.     //
  4245.     // Now process the body of the anonymous class !!!
  4246.     //
  4247.     CheckClassMembers(inner_type, class_body);
  4248.     ProcessNestedTypeHeaders(inner_type, class_body);
  4249.     if (inner_type -> owner -> MethodCast())
  4250.          ProcessSuperTypesOfOuterType(inner_type);
  4251.     else ProcessNestedSuperTypes(inner_type);
  4252.  
  4253.     //
  4254.     // If the class body has not yet been parsed, do so now.
  4255.     //
  4256.     if (class_body -> UnparsedClassBodyCast())
  4257.     {
  4258.         if (! control.parser -> InitializerParse(lex_stream, class_body))
  4259.              compilation_unit -> kind = Ast::BAD_COMPILATION; // mark the fact that syntax errors were detected
  4260.         else
  4261.         {
  4262.             inner_type -> MarkHeaderProcessed();
  4263.             ProcessMembers(inner_type -> semantic_environment, class_body);
  4264.             CompleteSymbolTable(inner_type -> semantic_environment, class_body -> left_brace_token, class_body);
  4265.         }
  4266.  
  4267.         if (! control.parser -> BodyParse(lex_stream, class_body))
  4268.              compilation_unit -> kind = Ast::BAD_COMPILATION; // mark the fact that syntax errors were detected
  4269.         else ProcessExecutableBodies(inner_type -> semantic_environment, class_body);
  4270.     }
  4271.     else // The relevant bodies have already been parsed
  4272.     {
  4273.         inner_type -> MarkHeaderProcessed();
  4274.         ProcessMembers(inner_type -> semantic_environment, class_body);
  4275.         CompleteSymbolTable(inner_type -> semantic_environment, class_body -> left_brace_token, class_body);
  4276.         ProcessExecutableBodies(inner_type -> semantic_environment, class_body);
  4277.     }
  4278.  
  4279.     //
  4280.     // Add the default constructor to the body of the anonymous type.
  4281.     // If the symbol was resolved to "no_type" then constructor will be NULL
  4282.     //
  4283.     MethodSymbol *constructor = class_creation -> class_type -> symbol -> MethodCast();
  4284.     if (constructor)
  4285.     {
  4286.         class_body -> default_constructor = (AstConstructorDeclaration *) constructor -> method_or_constructor_declaration;
  4287.  
  4288.         if (inner_type -> IsLocal())
  4289.         {
  4290.             inner_type -> AddLocalConstructorCallEnvironment(GetEnvironment(class_creation));
  4291.             UpdateLocalConstructors(inner_type);
  4292.         }
  4293.     }
  4294.  
  4295.     //
  4296.     // TODO: See comment above regarding the setting of this flag.
  4297.     //
  4298.     inner_type -> ResetACC_FINAL();
  4299.  
  4300.     return inner_type;
  4301. }
  4302.  
  4303.  
  4304. void Semantic::ProcessClassInstanceCreationExpression(Ast *expr)
  4305. {
  4306.     AstClassInstanceCreationExpression *class_creation = (AstClassInstanceCreationExpression *) expr;
  4307.  
  4308.     //
  4309.     // TODO: Is this needed ?
  4310.     //
  4311.     // This operation may throw OutOfMemoryError
  4312.     //
  4313.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  4314.     if (exception_set)
  4315.     {
  4316.         exception_set -> AddElement(control.Error());
  4317.     }
  4318.  
  4319.     Ast *actual_type = class_creation -> class_type -> type;
  4320.     TypeSymbol *type;
  4321.     if (class_creation -> base_opt)
  4322.     {
  4323.         ProcessExpression(class_creation -> base_opt);
  4324.  
  4325.         TypeSymbol *enclosing_type = class_creation -> base_opt -> Type();
  4326.         if (enclosing_type == control.no_type)
  4327.         {
  4328.             class_creation -> symbol = control.no_type;
  4329.             return;
  4330.         }
  4331.         else if (enclosing_type == control.null_type || enclosing_type -> Primitive())
  4332.         {
  4333.             ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  4334.                            class_creation -> base_opt -> LeftToken(),
  4335.                            class_creation -> base_opt -> RightToken(),
  4336.                            enclosing_type -> ExternalName());
  4337.             class_creation -> symbol = control.no_type;
  4338.             return;
  4339.         }
  4340.  
  4341.         //
  4342.         // The grammar guarantees that the actual type is a simple name.
  4343.         //
  4344.         type = MustFindNestedType(enclosing_type, actual_type);
  4345.         if (type -> ACC_STATIC())
  4346.         {
  4347.             ReportSemError(SemanticError::STATIC_NOT_INNER_CLASS,
  4348.                            actual_type -> LeftToken(),
  4349.                            actual_type -> RightToken(),
  4350.                            type -> ContainingPackage() -> PackageName(),
  4351.                            type -> ExternalName());
  4352.         }
  4353.     }
  4354.     else
  4355.     {
  4356.         type = MustFindType(actual_type);
  4357.         if (type -> IsInner())
  4358.         {
  4359.             //
  4360.             // Within an explicit constructor invocation, a class that is immediately nested
  4361.             // in the class being created is not accessible.
  4362.             //
  4363.             if (ExplicitConstructorInvocation() && type -> ContainingType() == ThisType())
  4364.             {
  4365.                 ReportSemError(SemanticError::INNER_CONSTRUCTOR_IN_EXPLICIT_CONSTRUCTOR_INVOCATION,
  4366.                                class_creation -> LeftToken(),
  4367.                                class_creation -> RightToken(),
  4368.                                type -> ContainingPackage() -> PackageName(),
  4369.                                type -> ExternalName(),
  4370.                                ThisType() -> ContainingPackage() -> PackageName(),
  4371.                                ThisType() -> ExternalName());
  4372.                 class_creation -> symbol = control.no_type;
  4373.                 return;
  4374.             }
  4375.  
  4376.             class_creation -> base_opt = CreateAccessToType(class_creation, type -> ContainingType());
  4377.         }
  4378.     }
  4379.  
  4380.     bool no_bad_argument = true;
  4381.     for (int i = 0; i < class_creation -> NumArguments(); i++)
  4382.     {
  4383.         AstExpression *expr = class_creation -> Argument(i);
  4384.         ProcessExpressionOrStringConstant(expr);
  4385.         no_bad_argument = no_bad_argument && (expr -> symbol != control.no_type);
  4386.     }
  4387.  
  4388.     TypeSymbol *anonymous_type = NULL;
  4389.  
  4390.     if (! no_bad_argument)
  4391.     {
  4392.         class_creation -> class_type -> symbol = control.no_type;
  4393.         class_creation -> symbol = type;
  4394.     }
  4395.     else
  4396.     {
  4397.         MethodSymbol *method = FindConstructor((type -> ACC_INTERFACE() ? control.Object() : type),
  4398.                                                class_creation,
  4399.                                                actual_type -> LeftToken(),
  4400.                                                class_creation -> right_parenthesis_token);
  4401.  
  4402.         if (! method)
  4403.         {
  4404.             class_creation -> class_type -> symbol = control.no_type;
  4405.             class_creation -> symbol = type;
  4406.         }
  4407.         else
  4408.         {
  4409.             assert(method -> IsTyped());
  4410.  
  4411.             if (class_creation -> base_opt &&
  4412.                 (class_creation -> base_opt -> symbol != control.no_type) &&
  4413.                 (class_creation -> base_opt -> Type() != method -> containing_type -> ContainingType()))
  4414.             {
  4415.                 assert(method -> containing_type);
  4416.                 assert(method -> containing_type -> ContainingType());
  4417.                 assert(class_creation -> base_opt -> Type());
  4418.                 assert(CanMethodInvocationConvert(method -> containing_type -> ContainingType(),
  4419.                                                   class_creation -> base_opt -> Type()));
  4420.  
  4421.                 class_creation -> base_opt = ConvertToType(class_creation -> base_opt, method -> containing_type -> ContainingType());
  4422.             }
  4423.  
  4424.             for (int i = 0; i < class_creation -> NumArguments(); i++)
  4425.             {
  4426.                 AstExpression *expr = class_creation -> Argument(i);
  4427.                 if (expr -> Type() != method -> FormalParameter(i) -> Type())
  4428.                     class_creation -> Argument(i) = ConvertToType(expr, method -> FormalParameter(i) -> Type());
  4429.             }
  4430.  
  4431.             if (class_creation -> class_body_opt)
  4432.                 anonymous_type = GetAnonymousType(class_creation, type);
  4433.             else
  4434.             {
  4435.                 if (type -> ACC_INTERFACE())
  4436.                 {
  4437.                     ReportSemError(SemanticError::NOT_A_CLASS,
  4438.                                    actual_type -> LeftToken(),
  4439.                                    actual_type -> RightToken(),
  4440.                                    type -> ContainingPackage() -> PackageName(),
  4441.                                    type -> ExternalName());
  4442.                     class_creation -> symbol = control.no_type;
  4443.                     return;
  4444.                 }
  4445.                 else if (type -> ACC_ABSTRACT())
  4446.                 {
  4447.                     ReportSemError(SemanticError::ABSTRACT_TYPE_CREATION,
  4448.                                    actual_type -> LeftToken(),
  4449.                                    actual_type -> RightToken(),
  4450.                                    type -> ExternalName());
  4451.                 }
  4452.  
  4453.                 class_creation -> class_type -> symbol = method;
  4454.  
  4455.                 if (exception_set)
  4456.                 {
  4457.                     for (int i = method -> NumThrows() - 1; i >= 0; i--)
  4458.                         exception_set -> AddElement(method -> Throws(i));
  4459.                 }
  4460.  
  4461.                 if (! (ThisType() -> Anonymous() && ThisMethod() && ThisMethod() -> Identity() == control.block_init_name_symbol))
  4462.                 {
  4463.                     for (int k = method -> NumThrows() - 1; k >= 0; k--)
  4464.                     {
  4465.                         TypeSymbol *exception = method -> Throws(k);
  4466.                         if (! CatchableException(exception))
  4467.                         {
  4468.                             ReportSemError(SemanticError::UNCATCHABLE_CONSTRUCTOR_THROWN_CHECKED_EXCEPTION,
  4469.                                            actual_type -> LeftToken(),
  4470.                                            actual_type -> RightToken(),
  4471.                                            type -> ExternalName(),
  4472.                                            exception -> ContainingPackage() -> PackageName(),
  4473.                                            exception -> ExternalName());
  4474.                         }
  4475.                     }
  4476.                 }
  4477.             }
  4478.  
  4479.             class_creation -> symbol = (anonymous_type ? anonymous_type : type);
  4480.  
  4481.             //
  4482.             // Note that since constructors are not inherited, we do not need
  4483.             // to worry about the protected case here.
  4484.             //
  4485.             if (ThisType() != type &&
  4486.                 ThisType() -> outermost_type == type -> outermost_type &&
  4487.                 method -> ACC_PRIVATE())
  4488.             {
  4489.                 if (! method -> LocalConstructor())
  4490.                 {
  4491.                     method = type -> GetReadAccessMethod(method);
  4492.                     class_creation -> class_type -> symbol = method;
  4493.  
  4494.                     //
  4495.                     // Add extra argument for read access constructor;
  4496.                     //
  4497.                     class_creation -> AddNullArgument();
  4498.                 }
  4499.             }
  4500.             else ConstructorAccessCheck(class_creation, method);
  4501.  
  4502.             //
  4503.             // A local type may use enclosed local variables. So, we at least allocate the
  4504.             // space for adding these extra arguments. If the type being created has already been
  4505.             // fully processed, add the extra arguments here.
  4506.             //
  4507.             if ((! anonymous_type) && type -> IsLocal())
  4508.             {
  4509.                 if (type -> LocalClassProcessingCompleted() && method -> LocalConstructor())
  4510.                 {
  4511.                     assert(! method -> IsGeneratedLocalConstructor());
  4512.  
  4513.                     class_creation -> class_type -> symbol = method -> LocalConstructor();
  4514.  
  4515.                     assert(method -> LocalConstructor() -> signature);
  4516.  
  4517.                     for (int i = (type -> ACC_STATIC() ? 0 : 1); i < type -> NumConstructorParameters(); i++)
  4518.                     {
  4519.                         VariableSymbol *local = type -> ConstructorParameter(i) -> accessed_local;
  4520.  
  4521.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(class_creation -> new_token);
  4522.                         //
  4523.                         // Are we currently within the body of the method that contains
  4524.                         // the local type in question?
  4525.                         //
  4526.                         simple_name -> symbol = (type -> owner == ThisMethod()
  4527.                                                                 ? local
  4528.                                                                 : ThisType() -> FindOrInsertLocalShadow(local));
  4529.                         class_creation -> AddLocalArgument(simple_name);
  4530.                     }
  4531.                 }
  4532.                 else // are we within body of type in question? Save processing for later. See ProcessClassDeclaration in body.cpp
  4533.                     type -> AddLocalConstructorCallEnvironment(GetEnvironment(class_creation));
  4534.             }
  4535.         }
  4536.     }
  4537.  
  4538.     return;
  4539. }
  4540.  
  4541.  
  4542. void Semantic::ProcessArrayCreationExpression(Ast *expr)
  4543. {
  4544.     AstArrayCreationExpression *array_creation = (AstArrayCreationExpression *) expr;
  4545.  
  4546.     //
  4547.     // TODO: Is this needed ?
  4548.     //
  4549.     // This operation may throw OutOfMemoryError or NegativeArraySizeException
  4550.     //
  4551.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  4552.     if (exception_set)
  4553.     {
  4554.         exception_set -> AddElement(control.RuntimeException());
  4555.         exception_set -> AddElement(control.Error());
  4556.     }
  4557.  
  4558.     AstArrayType *array_type;
  4559.  
  4560.     TypeSymbol *type;
  4561.  
  4562.     if ((array_type = array_creation -> array_type -> ArrayTypeCast()))
  4563.     {
  4564.         AstPrimitiveType *primitive_type = array_type -> type -> PrimitiveTypeCast();
  4565.         type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_type -> type));
  4566.     }
  4567.     else
  4568.     {
  4569.         AstPrimitiveType *primitive_type = array_creation -> array_type -> PrimitiveTypeCast();
  4570.         type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(array_creation -> array_type));
  4571.     }
  4572.  
  4573.     int num_dimensions = (array_type ? array_type -> NumBrackets()
  4574.                                      : array_creation -> NumDimExprs() + array_creation -> NumBrackets());
  4575.  
  4576.     if (num_dimensions > 0)
  4577.         type = type -> GetArrayType((Semantic *) this, num_dimensions);
  4578.     array_creation -> symbol = type;
  4579.  
  4580.     for (int i = 0; i < array_creation -> NumDimExprs(); i++)
  4581.     {
  4582.         AstDimExpr *dim_expr = array_creation -> DimExpr(i);
  4583.         ProcessExpression(dim_expr -> expression);
  4584.         AstExpression *expr = PromoteUnaryNumericExpression(dim_expr -> expression);
  4585.         if (expr -> Type() != control.int_type && expr -> symbol != control.no_type)
  4586.         {
  4587.             ReportSemError(SemanticError::TYPE_NOT_INTEGER,
  4588.                            dim_expr -> expression -> LeftToken(),
  4589.                            dim_expr -> expression -> RightToken(),
  4590.                            dim_expr -> expression -> Type() -> Name());
  4591.         }
  4592.         dim_expr -> expression = expr;
  4593.     }
  4594.  
  4595.     if (array_creation -> array_initializer_opt)
  4596.         ProcessArrayInitializer((AstArrayInitializer *) array_creation -> array_initializer_opt, type);
  4597.  
  4598.     return;
  4599. }
  4600.  
  4601.  
  4602. void Semantic::ProcessPostUnaryExpression(Ast *expr)
  4603. {
  4604.     AstPostUnaryExpression *postfix_expression = (AstPostUnaryExpression *) expr;
  4605.  
  4606.     ProcessExpression(postfix_expression -> expression);
  4607.     postfix_expression -> symbol = postfix_expression -> expression -> symbol;
  4608.  
  4609.     if (postfix_expression -> symbol != control.no_type)
  4610.     {
  4611.         if (! postfix_expression -> expression -> IsLeftHandSide())
  4612.         {
  4613.             ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
  4614.                            postfix_expression -> expression -> LeftToken(),
  4615.                            postfix_expression -> expression -> RightToken(),
  4616.                            postfix_expression -> expression -> Type() -> Name());
  4617.             postfix_expression -> symbol = control.no_type;
  4618.         }
  4619.         else if (! control.IsNumeric(postfix_expression -> Type()))
  4620.         {
  4621.             ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  4622.                            postfix_expression -> expression -> LeftToken(),
  4623.                            postfix_expression -> expression -> RightToken(),
  4624.                            postfix_expression -> Type() -> Name());
  4625.             postfix_expression -> symbol = control.no_type;
  4626.         }
  4627.         else if (! postfix_expression -> expression -> ArrayAccessCast()) // some kind of name
  4628.         {
  4629.             MethodSymbol *read_method = NULL;
  4630.             AstSimpleName *simple_name = postfix_expression -> expression -> SimpleNameCast();
  4631.             if (simple_name)
  4632.             {
  4633.                 if (simple_name -> resolution_opt)
  4634.                    read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  4635.             }
  4636.             else
  4637.             {
  4638.                 AstFieldAccess *field_access = (AstFieldAccess *) postfix_expression -> expression;
  4639.                 if (field_access -> resolution_opt)
  4640.                     read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  4641.             }
  4642.  
  4643.             VariableSymbol *variable_symbol;
  4644.             if (read_method)
  4645.             {
  4646.                 variable_symbol = (VariableSymbol *) read_method -> accessed_member;
  4647.                 postfix_expression -> write_method = read_method -> containing_type -> GetWriteAccessMethod(variable_symbol);
  4648.             }
  4649.             else variable_symbol = postfix_expression -> expression -> symbol -> VariableCast();
  4650.         }
  4651.     }
  4652.  
  4653.     return;
  4654. }
  4655.  
  4656.  
  4657. void Semantic::ProcessPLUS(AstPreUnaryExpression *expr)
  4658. {
  4659.     ProcessExpression(expr -> expression);
  4660.     expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4661.     expr -> value = expr -> expression -> value;
  4662.     expr -> symbol = expr -> expression -> symbol;
  4663. }
  4664.  
  4665.  
  4666. void Semantic::ProcessMINUS(AstPreUnaryExpression *expr)
  4667. {
  4668.     AstIntegerLiteral *int_literal;
  4669.     AstLongLiteral *long_literal;
  4670.  
  4671.     if ((int_literal = expr -> expression -> IntegerLiteralCast()))
  4672.     {
  4673.         LiteralSymbol *literal = lex_stream -> LiteralSymbol(int_literal -> integer_literal_token);
  4674.  
  4675.         expr -> value = control.int_pool.FindOrInsertNegativeInt(literal);
  4676.         if (expr -> value == control.BadValue())
  4677.         {
  4678.             ReportSemError(SemanticError::INVALID_INT_VALUE,
  4679.                            expr -> LeftToken(),
  4680.                            expr -> RightToken());
  4681.             expr -> symbol = control.no_type;
  4682.         }
  4683.         else expr -> symbol = control.int_type;
  4684.     }
  4685.     else if ((long_literal = expr -> expression -> LongLiteralCast()))
  4686.     {
  4687.         LiteralSymbol *literal = lex_stream -> LiteralSymbol(long_literal -> long_literal_token);
  4688.  
  4689.         expr -> value = control.long_pool.FindOrInsertNegativeLong(literal);
  4690.         if (expr -> value == control.BadValue())
  4691.         {
  4692.             ReportSemError(SemanticError::INVALID_LONG_VALUE,
  4693.                            expr -> LeftToken(),
  4694.                            expr -> RightToken());
  4695.             expr -> symbol = control.no_type;
  4696.         }
  4697.         else expr -> symbol = control.long_type;
  4698.     }
  4699.     else
  4700.     {
  4701.         ProcessExpression(expr -> expression);
  4702.  
  4703.         expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4704.         expr -> symbol = expr -> expression -> symbol;
  4705.         if (expr -> expression -> IsConstant())
  4706.         {
  4707.             TypeSymbol *type = expr -> Type();
  4708.  
  4709.             if ((type == control.double_type))
  4710.             {
  4711.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> expression -> value;
  4712.                 expr -> value = control.double_pool.FindOrInsert(-literal -> value);
  4713.             }
  4714.             else if ((type == control.float_type))
  4715.             {
  4716.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> expression -> value;
  4717.                 expr -> value = control.float_pool.FindOrInsert(-literal -> value);
  4718.             }
  4719.             else if ((type == control.long_type))
  4720.             {
  4721.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> expression -> value;
  4722.                 expr -> value = control.long_pool.FindOrInsert(-literal -> value);
  4723.             }
  4724.             else
  4725.             {
  4726.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4727.                 expr -> value = control.int_pool.FindOrInsert(-literal -> value);
  4728.             }
  4729.         }
  4730.     }
  4731.  
  4732.     return;
  4733. }
  4734.  
  4735.  
  4736. void Semantic::ProcessTWIDDLE(AstPreUnaryExpression *expr)
  4737. {
  4738.     ProcessExpression(expr -> expression);
  4739.  
  4740.     if (expr -> expression -> symbol != control.no_type && (! control.IsIntegral(expr -> expression -> Type())))
  4741.     {
  4742.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  4743.                        expr -> expression -> LeftToken(),
  4744.                        expr -> expression -> RightToken(),
  4745.                        expr -> expression -> Type() -> Name());
  4746.         expr -> symbol = control.no_type;
  4747.     }
  4748.     else
  4749.     {
  4750.         expr -> expression = PromoteUnaryNumericExpression(expr -> expression);
  4751.  
  4752.         if (expr -> expression -> IsConstant())
  4753.         {
  4754.             if (expr -> expression -> Type() == control.long_type)
  4755.             {
  4756.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> expression -> value;
  4757.                 expr -> value = control.long_pool.FindOrInsert(~literal -> value);
  4758.             }
  4759.             else // assert(expr -> expression -> Type() == control.int_type)
  4760.             {
  4761.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4762.                 expr -> value = control.int_pool.FindOrInsert(~literal -> value);
  4763.             }
  4764.         }
  4765.         expr -> symbol = expr -> expression -> symbol;
  4766.     }
  4767.  
  4768.     return;
  4769. }
  4770.  
  4771.  
  4772. void Semantic::ProcessNOT(AstPreUnaryExpression *expr)
  4773. {
  4774.     ProcessExpression(expr -> expression);
  4775.  
  4776.     if (expr -> expression -> symbol != control.no_type && expr -> expression -> Type() != control.boolean_type)
  4777.     {
  4778.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  4779.                        expr -> expression -> LeftToken(),
  4780.                        expr -> expression -> RightToken(),
  4781.                        expr -> expression -> Type() -> Name());
  4782.         expr -> symbol = control.no_type;
  4783.     }
  4784.     else
  4785.     {
  4786.         if (expr -> expression -> IsConstant())
  4787.         {
  4788.             IntLiteralValue *literal = (IntLiteralValue *) expr -> expression -> value;
  4789.             expr -> value = control.int_pool.FindOrInsert(literal -> value ? 0 : 1);
  4790.         }
  4791.         expr -> symbol = control.boolean_type;
  4792.     }
  4793.  
  4794.     return;
  4795. }
  4796.  
  4797.  
  4798. void Semantic::ProcessPLUSPLUSOrMINUSMINUS(AstPreUnaryExpression *expr)
  4799. {
  4800.     ProcessExpression(expr -> expression);
  4801.  
  4802.     if (expr -> expression -> symbol != control.no_type)
  4803.     {
  4804.         if (! expr -> expression -> IsLeftHandSide())
  4805.         {
  4806.             ReportSemError(SemanticError::NOT_A_NUMERIC_VARIABLE,
  4807.                            expr -> expression -> LeftToken(),
  4808.                            expr -> expression -> RightToken(),
  4809.                            expr -> expression -> Type() -> Name());
  4810.             expr -> symbol = control.no_type;
  4811.         }
  4812.         else if (! control.IsNumeric(expr -> expression -> Type()))
  4813.         {
  4814.             ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  4815.                            expr -> expression -> LeftToken(),
  4816.                            expr -> expression -> RightToken(),
  4817.                            expr -> expression -> Type() -> Name());
  4818.             expr -> symbol = control.no_type;
  4819.         }
  4820.         else if (! expr -> expression -> ArrayAccessCast()) // some kind of name
  4821.         {
  4822.             MethodSymbol *read_method = NULL;
  4823.             AstSimpleName *simple_name = expr -> expression -> SimpleNameCast();
  4824.             if (simple_name)
  4825.             {
  4826.                 if (simple_name -> resolution_opt)
  4827.                    read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  4828.             }
  4829.             else
  4830.             {
  4831.                 AstFieldAccess *field_access = (AstFieldAccess *) expr -> expression;
  4832.                 if (field_access -> resolution_opt)
  4833.                     read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  4834.             }
  4835.  
  4836.             VariableSymbol *variable_symbol;
  4837.             if (read_method)
  4838.             {
  4839.                 variable_symbol = (VariableSymbol *) read_method -> accessed_member;
  4840.                 expr -> write_method = read_method -> containing_type -> GetWriteAccessMethod(variable_symbol);
  4841.             }
  4842.             else variable_symbol = expr -> expression -> symbol -> VariableCast();
  4843.         }
  4844.     }
  4845.     expr -> symbol = expr -> expression -> symbol;
  4846.  
  4847.     return;
  4848. }
  4849.  
  4850.  
  4851. void Semantic::ProcessPreUnaryExpression(Ast *expr)
  4852. {
  4853.     AstPreUnaryExpression *prefix_expression = (AstPreUnaryExpression *) expr;
  4854.     (this ->* ProcessPreUnaryExpr[prefix_expression -> pre_unary_tag])(prefix_expression);
  4855.  
  4856.     return;
  4857. }
  4858.  
  4859.  
  4860. inline bool Semantic::CanWideningPrimitiveConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4861. {
  4862.     if (target_type == control.double_type)
  4863.          return (source_type == control.float_type || source_type == control.long_type  || source_type == control.int_type ||
  4864.                  source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4865.     else if (target_type == control.float_type)
  4866.          return (source_type == control.long_type  || source_type == control.int_type   ||
  4867.                  source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4868.     else if (target_type == control.long_type)
  4869.          return (source_type == control.int_type   || source_type == control.char_type  ||
  4870.                  source_type == control.short_type || source_type == control.byte_type);
  4871.     else if (target_type == control.int_type)
  4872.          return (source_type == control.char_type  || source_type == control.short_type || source_type == control.byte_type);
  4873.     else if (target_type == control.short_type)
  4874.          return source_type == control.byte_type;
  4875.  
  4876.     return false;
  4877. }
  4878.  
  4879.  
  4880. inline bool Semantic::CanNarrowingPrimitiveConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4881. {
  4882.     if (target_type == control.byte_type)
  4883.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type ||
  4884.                  source_type == control.int_type    || source_type == control.char_type  || source_type == control.short_type);
  4885.     else if (target_type == control.char_type)
  4886.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type ||
  4887.                  source_type == control.int_type    || source_type == control.short_type || source_type == control.byte_type);
  4888.     else if (target_type == control.short_type)
  4889.          return (source_type == control.double_type || source_type == control.float_type ||
  4890.                  source_type == control.long_type   || source_type == control.int_type   || source_type == control.char_type);
  4891.     else if (target_type == control.int_type)
  4892.          return (source_type == control.double_type || source_type == control.float_type || source_type == control.long_type);
  4893.     else if (target_type == control.long_type)
  4894.          return (source_type == control.double_type || source_type == control.float_type);
  4895.     else if (target_type == control.float_type)
  4896.          return source_type == control.double_type;
  4897.  
  4898.     return false;
  4899. }
  4900.  
  4901.  
  4902. bool Semantic::CanMethodInvocationConvert(TypeSymbol *target_type, TypeSymbol *source_type)
  4903. {
  4904.     if (target_type == control.no_type || source_type == control.no_type)
  4905.         return false;
  4906.  
  4907.     if (source_type -> Primitive())
  4908.     {
  4909.         if (! target_type -> Primitive())
  4910.             return false;
  4911.  
  4912.         return (target_type == source_type || CanWideningPrimitiveConvert(target_type, source_type));
  4913.     }
  4914.     else
  4915.     {
  4916.         if (target_type -> Primitive())
  4917.             return false;
  4918.  
  4919.         if (source_type -> IsArray())
  4920.         {
  4921.             if (target_type -> IsArray())
  4922.             {
  4923.                 TypeSymbol *source_subtype = source_type -> ArraySubtype();
  4924.                 TypeSymbol *target_subtype = target_type -> ArraySubtype();
  4925.                 return (source_subtype -> Primitive() && target_subtype -> Primitive()
  4926.                                                        ? (source_subtype == target_subtype)
  4927.                                                        : CanMethodInvocationConvert(target_subtype, source_subtype));
  4928.             }
  4929.             return (target_type == control.Object() ||
  4930.                     target_type == control.Cloneable() ||
  4931.                     //
  4932.                     // TODO: This is an undocumented feature, but this fix appears to make sense.
  4933.                     //
  4934.                     (target_type == control.Serializable() && source_type -> Implements(target_type)));
  4935.         }
  4936.         else if (source_type -> ACC_INTERFACE())
  4937.         {
  4938.             if (target_type -> ACC_INTERFACE())
  4939.                  return source_type -> IsSubinterface(target_type);
  4940.             else if (target_type != control.Object()) // target is a class type
  4941.                  return false;
  4942.         }
  4943.         else if (source_type != control.null_type) // source_type is a class
  4944.         {
  4945.             if (target_type -> IsArray())
  4946.                  return false;
  4947.             else if (target_type -> ACC_INTERFACE())
  4948.                  return source_type -> Implements(target_type);
  4949.             else if (! source_type -> IsSubclass(target_type))
  4950.                  return false;
  4951.         }
  4952.     }
  4953.  
  4954.     return true;
  4955. }
  4956.  
  4957.  
  4958. bool Semantic::CanAssignmentConvertReference(TypeSymbol *target_type, TypeSymbol *source_type)
  4959. {
  4960.     return (target_type == control.no_type ||
  4961.             source_type == control.no_type ||
  4962.             CanMethodInvocationConvert(target_type, source_type)
  4963.            );
  4964. }
  4965.  
  4966.  
  4967. bool Semantic::CanAssignmentConvert(TypeSymbol *target_type, AstExpression *expr)
  4968. {
  4969.     return (target_type == control.no_type ||
  4970.             expr -> symbol == control.no_type ||
  4971.             CanMethodInvocationConvert(target_type, expr -> Type()) ||
  4972.             IsIntValueRepresentableInType(expr, target_type)
  4973.            );
  4974. }
  4975.  
  4976.  
  4977. bool Semantic::CanCastConvert(TypeSymbol *target_type, TypeSymbol *source_type, LexStream::TokenIndex tok)
  4978. {
  4979.     if (source_type == target_type || source_type == control.no_type || target_type == control.no_type)
  4980.         return true;
  4981.  
  4982.     if (source_type -> Primitive())
  4983.     {
  4984.         if (! target_type -> Primitive())
  4985.             return false;
  4986.  
  4987.         return (CanWideningPrimitiveConvert(target_type, source_type) || CanNarrowingPrimitiveConvert(target_type, source_type));
  4988.     }
  4989.     else
  4990.     {
  4991.         if (target_type -> Primitive())
  4992.             return false;
  4993.  
  4994.         if (source_type -> IsArray())
  4995.         {
  4996.             if (target_type -> IsArray())
  4997.             {
  4998.                 TypeSymbol *source_subtype = source_type -> ArraySubtype();
  4999.                 TypeSymbol *target_subtype = target_type -> ArraySubtype();
  5000.                 return (source_subtype -> Primitive() && target_subtype -> Primitive()
  5001.                                                        ? (source_subtype == target_subtype)
  5002.                                                        : CanCastConvert(target_subtype, source_subtype, tok));
  5003.             }
  5004.             return (target_type == control.Object() ||
  5005.                     target_type == control.Cloneable() ||
  5006.                     //
  5007.                     // TODO: This is an undocumented feature, but this fix appears to make sense.
  5008.                     //
  5009.                     (target_type == control.Serializable() && source_type -> Implements(target_type)));
  5010.         }
  5011.         else if (source_type -> ACC_INTERFACE())
  5012.         {
  5013.             if (target_type -> ACC_INTERFACE())
  5014.             {
  5015.                 if (! source_type -> expanded_method_table)
  5016.                     ComputeMethodsClosure(source_type, tok);
  5017.                 if (! target_type -> expanded_method_table)
  5018.                     ComputeMethodsClosure(target_type, tok);
  5019.  
  5020.                 //
  5021.                 // Iterate over all methods in the source symbol table of the source_type interface;
  5022.                 // For each such method, if the target_type contains a method with the same signature,
  5023.                 // then make sure that the two methods have the same return type.
  5024.                 //
  5025.                 ExpandedMethodTable *source_method_table = source_type -> expanded_method_table;
  5026.                 int i;
  5027.                 for (i = 0; i < source_method_table -> symbol_pool.Length(); i++)
  5028.                 {
  5029.                     MethodSymbol *method1 = source_method_table -> symbol_pool[i] -> method_symbol;
  5030.                     MethodShadowSymbol *method_shadow2 = target_type -> expanded_method_table
  5031.                                                                      -> FindOverloadMethodShadow(method1, (Semantic *) this, tok);
  5032.                     if (method_shadow2)
  5033.                     {
  5034.                         if (! method1 -> IsTyped())
  5035.                             method1 -> ProcessMethodSignature((Semantic *) this, tok);
  5036.  
  5037.                         MethodSymbol *method2 = method_shadow2 -> method_symbol;
  5038.                         if (! method2 -> IsTyped())
  5039.                             method2 -> ProcessMethodSignature((Semantic *) this, tok);
  5040.                         if (method1 -> Type() != method2 -> Type())
  5041.                             break;
  5042.                     }
  5043.                 }
  5044.  
  5045.                 return (i == source_method_table -> symbol_pool.Length()); // all the methods passed the test
  5046.             }
  5047.             else if (target_type -> ACC_FINAL() && (! target_type -> Implements(source_type)))
  5048.                  return false;
  5049.         }
  5050.         else if (source_type != control.null_type) // source_type is a class
  5051.         {
  5052.             if (target_type -> IsArray())
  5053.             {
  5054.                 if (source_type != control.Object())
  5055.                     return false;
  5056.             }
  5057.             else if (target_type -> ACC_INTERFACE())
  5058.             {
  5059.                 if (source_type -> ACC_FINAL() && (! source_type -> Implements(target_type)))
  5060.                     return false;
  5061.             }
  5062.             else if ((! source_type -> IsSubclass(target_type)) && (! target_type -> IsSubclass(source_type)))
  5063.                  return false;
  5064.         }
  5065.     }
  5066.  
  5067.     return true;
  5068. }
  5069.  
  5070.  
  5071. LiteralValue *Semantic::CastPrimitiveValue(TypeSymbol *target_type, AstExpression *expr)
  5072. {
  5073.     LiteralValue *literal_value = NULL;
  5074.     TypeSymbol *source_type = expr -> Type();
  5075.  
  5076.     if (target_type == source_type)
  5077.         literal_value = expr -> value;
  5078.     else if (source_type != control.no_type)
  5079.     {
  5080.         char output_string[25];
  5081.         int len;
  5082.  
  5083.         if (target_type == control.String())
  5084.         {
  5085.             if (source_type == control.double_type)
  5086.             {
  5087.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5088.                 DoubleToString ieee_double(literal -> value);
  5089.                 literal_value = control.Utf8_pool.FindOrInsert(ieee_double.String(), ieee_double.Length());
  5090.             }
  5091.             else if (source_type == control.float_type)
  5092.             {
  5093.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5094.                 FloatToString ieee_float(literal -> value);
  5095.                 literal_value = control.Utf8_pool.FindOrInsert(ieee_float.String(), ieee_float.Length());
  5096.             }
  5097.             else if (source_type == control.long_type)
  5098.             {
  5099.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5100.                 LongToDecString long_integer(literal -> value);
  5101.                 literal_value = control.Utf8_pool.FindOrInsert(long_integer.String(), long_integer.Length());
  5102.             }
  5103.             else if (source_type == control.char_type)
  5104.             {
  5105.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5106.                 literal_value = control.Utf8_pool.FindOrInsert(literal -> value);
  5107.             }
  5108.             else if (source_type == control.boolean_type)
  5109.             {
  5110.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5111.                 if (literal -> value == 0)
  5112.                 {
  5113.                     output_string[0] = U_f;
  5114.                     output_string[1] = U_a;
  5115.                     output_string[2] = U_l;
  5116.                     output_string[3] = U_s;
  5117.                     output_string[4] = U_e;
  5118.                     len = 5;
  5119.                 }
  5120.                 else
  5121.                 {
  5122.                     output_string[0] = U_t;
  5123.                     output_string[1] = U_r;
  5124.                     output_string[2] = U_u;
  5125.                     output_string[3] = U_e;
  5126.                     len = 4;
  5127.                 }
  5128.                 literal_value = control.Utf8_pool.FindOrInsert(output_string, len);
  5129.             }
  5130.             else if (control.IsSimpleIntegerValueType(source_type))
  5131.             {
  5132.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5133.                 IntToString integer(literal -> value);
  5134.                 literal_value = control.Utf8_pool.FindOrInsert(integer.String(), integer.Length());
  5135.             }
  5136.             else if (expr -> value == control.NullValue())
  5137.                 literal_value = expr -> value;
  5138.         }
  5139.         else if (target_type == control.double_type)
  5140.         {
  5141.             if (source_type == control.float_type)
  5142.             {
  5143.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5144.                 IEEEdouble value(literal -> value);
  5145.                 literal_value = control.double_pool.FindOrInsert(value);
  5146.             }
  5147.             else if (source_type == control.long_type)
  5148.             {
  5149.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5150.                 IEEEdouble value(literal -> value);
  5151.                 literal_value = control.double_pool.FindOrInsert(value);
  5152.             }
  5153.             else
  5154.             {
  5155.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5156.                 IEEEdouble value(literal -> value);
  5157.                 literal_value = control.double_pool.FindOrInsert(value);
  5158.             }
  5159.         }
  5160.         else if (target_type == control.float_type)
  5161.         {
  5162.             if (source_type == control.double_type)
  5163.             {
  5164.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5165.                 IEEEfloat value(literal -> value);
  5166.                 literal_value = control.float_pool.FindOrInsert(value);
  5167.             }
  5168.             else if (source_type == control.long_type)
  5169.             {
  5170.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5171.                 IEEEfloat value(literal -> value);
  5172.                 literal_value = control.float_pool.FindOrInsert(value);
  5173.             }
  5174.             else
  5175.             {
  5176.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5177.                 IEEEfloat value(literal -> value);
  5178.                 literal_value = control.float_pool.FindOrInsert(value);
  5179.             }
  5180.         }
  5181.         else if (target_type == control.long_type)
  5182.         {
  5183.             if (source_type == control.double_type)
  5184.             {
  5185.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5186.                 LongInt value(literal -> value);
  5187.                 literal_value = control.long_pool.FindOrInsert(value);
  5188.             }
  5189.             else if (source_type == control.float_type)
  5190.             {
  5191.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5192.                 LongInt value(literal -> value);
  5193.                 literal_value = control.long_pool.FindOrInsert(value);
  5194.             }
  5195.             else
  5196.             {
  5197.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5198.                 literal_value = control.long_pool.FindOrInsert((LongInt) literal -> value);
  5199.             }
  5200.         }
  5201.         else if (target_type == control.int_type)
  5202.         {
  5203.             if (source_type == control.double_type)
  5204.             {
  5205.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5206.                 literal_value = control.int_pool.FindOrInsert((literal -> value).IntValue());
  5207.             }
  5208.             else if (source_type == control.float_type)
  5209.             {
  5210.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5211.                 literal_value = control.int_pool.FindOrInsert(literal -> value.IntValue());
  5212.             }
  5213.             else if (source_type == control.long_type)
  5214.             {
  5215.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5216.                 literal_value = control.int_pool.FindOrInsert((int) (literal -> value).LowWord());
  5217.             }
  5218.             else literal_value = expr -> value;
  5219.         }
  5220.         else if (target_type == control.char_type)
  5221.         {
  5222.             if (source_type == control.double_type)
  5223.             {
  5224.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5225.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value.IntValue()));
  5226.             }
  5227.             else if (source_type == control.float_type)
  5228.             {
  5229.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5230.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value.IntValue()));
  5231.             }
  5232.             else if (source_type == control.long_type)
  5233.             {
  5234.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5235.                 literal_value = control.int_pool.FindOrInsert((int) (u2) (literal -> value).LowWord());
  5236.             }
  5237.             else
  5238.             {
  5239.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5240.                 literal_value = control.int_pool.FindOrInsert((int) (u2) literal -> value);
  5241.             }
  5242.         }
  5243.         else if (target_type == control.short_type)
  5244.         {
  5245.             if (source_type == control.double_type)
  5246.             {
  5247.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5248.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value.IntValue()));
  5249.             }
  5250.             else if (source_type == control.float_type)
  5251.             {
  5252.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5253.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value.IntValue()));
  5254.             }
  5255.             else if (source_type == control.long_type)
  5256.             {
  5257.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5258.                 literal_value = control.int_pool.FindOrInsert((int) (i2) (literal -> value).LowWord());
  5259.             }
  5260.             else
  5261.             {
  5262.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5263.                 literal_value = control.int_pool.FindOrInsert((int) (i2) literal -> value);
  5264.             }
  5265.         }
  5266.         else if (target_type == control.byte_type)
  5267.         {
  5268.             if (source_type == control.double_type)
  5269.             {
  5270.                 DoubleLiteralValue *literal = (DoubleLiteralValue *) expr -> value;
  5271.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value.IntValue()));
  5272.             }
  5273.             else if (source_type == control.float_type)
  5274.             {
  5275.                 FloatLiteralValue *literal = (FloatLiteralValue *) expr -> value;
  5276.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value.IntValue()));
  5277.             }
  5278.             else if (source_type == control.long_type)
  5279.             {
  5280.                 LongLiteralValue *literal = (LongLiteralValue *) expr -> value;
  5281.                 literal_value = control.int_pool.FindOrInsert((int) (i1) (literal -> value).LowWord());
  5282.             }
  5283.             else
  5284.             {
  5285.                 IntLiteralValue *literal = (IntLiteralValue *) expr -> value;
  5286.                 literal_value = control.int_pool.FindOrInsert((int) (i1) literal -> value);
  5287.             }
  5288.         }
  5289.     }
  5290.  
  5291.     return literal_value;
  5292. }
  5293.  
  5294.  
  5295. //
  5296. // We only need to cast the value of constant primitive expressions.
  5297. //
  5298. inline LiteralValue *Semantic::CastValue(TypeSymbol *target_type, AstExpression *expr)
  5299. {
  5300.     return (LiteralValue *) (expr -> IsConstant() && (target_type -> Primitive() || target_type == control.String())
  5301.                                                    ? CastPrimitiveValue(target_type, expr)
  5302.                                                    : NULL);
  5303. }
  5304.  
  5305.  
  5306. void Semantic::ProcessCastExpression(Ast *expr)
  5307. {
  5308.     AstCastExpression *cast_expression = (AstCastExpression *) expr;
  5309.  
  5310.     //
  5311.     // TODO: Is this needed ?
  5312.     //
  5313.     // This operation may throw ClassCastException
  5314.     //
  5315.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  5316.     if (exception_set)
  5317.     {
  5318.         exception_set -> AddElement(control.RuntimeException());
  5319.     }
  5320.  
  5321.     ProcessExpression(cast_expression -> expression);
  5322.  
  5323.     TypeSymbol *source_type = cast_expression -> expression -> Type();
  5324.  
  5325.     //
  5326.     // Recall that the type is optional only when the compiler inserts
  5327.     // a CAST conversion node into the program.
  5328.     //
  5329.     AstPrimitiveType *primitive_type = cast_expression -> type_opt -> PrimitiveTypeCast();
  5330.     TypeSymbol *target_type;
  5331.     if (primitive_type)
  5332.          target_type = FindPrimitiveType(primitive_type);
  5333.     else if (cast_expression -> type_opt -> IsName())
  5334.          target_type = MustFindType(cast_expression -> type_opt);
  5335.     else
  5336.     {
  5337.         ReportSemError(SemanticError::INVALID_CAST_TYPE,
  5338.                        cast_expression -> type_opt -> LeftToken(),
  5339.                        cast_expression -> type_opt -> RightToken());
  5340.         cast_expression -> symbol = control.no_type;
  5341.  
  5342.         return;
  5343.     }
  5344.  
  5345.     int num_dimensions = cast_expression -> NumBrackets();
  5346.     target_type = (num_dimensions == 0 ? target_type : target_type -> GetArrayType((Semantic *) this, num_dimensions));
  5347.  
  5348.     if (CanAssignmentConvert(target_type, cast_expression -> expression))
  5349.     {
  5350.         cast_expression -> symbol = target_type;
  5351.         cast_expression -> value = CastValue(target_type, cast_expression -> expression);
  5352.     }
  5353.     else if (CanCastConvert(target_type, source_type, cast_expression -> right_parenthesis_token_opt))
  5354.     {
  5355.         cast_expression -> kind = Ast::CHECK_AND_CAST;
  5356.         cast_expression -> symbol = target_type;
  5357.         cast_expression -> value = CastValue(target_type, cast_expression -> expression);
  5358.     }
  5359.     else
  5360.     {
  5361.         ReportSemError(SemanticError::INVALID_CAST_CONVERSION,
  5362.                        cast_expression -> expression -> LeftToken(),
  5363.                        cast_expression -> expression -> RightToken(),
  5364.                        source_type -> Name(),
  5365.                        target_type -> Name());
  5366.         cast_expression -> symbol = control.no_type;
  5367.     }
  5368.  
  5369.     return;
  5370. }
  5371.  
  5372.  
  5373. AstExpression *Semantic::ConvertToType(AstExpression *expr, TypeSymbol *type)
  5374. {
  5375.     if (expr -> Type() == control.null_type)
  5376.         return expr;
  5377.  
  5378.     LexStream::TokenIndex loc = expr -> LeftToken();
  5379.  
  5380.     AstCastExpression *result = compilation_unit -> ast_pool -> GenCastExpression();
  5381.     result -> left_parenthesis_token_opt = loc;
  5382.     result -> type_opt = NULL;
  5383.     result -> right_parenthesis_token_opt = loc;
  5384.     result -> expression = expr;
  5385.  
  5386.     result -> symbol = type;
  5387.     result -> value = CastValue(type, expr);
  5388.  
  5389.     return result;
  5390. }
  5391.  
  5392.  
  5393. AstExpression *Semantic::PromoteUnaryNumericExpression(AstExpression *unary_expression)
  5394. {
  5395.     TypeSymbol *type = unary_expression -> Type();
  5396.  
  5397.     if (type == control.no_type)
  5398.         return unary_expression;
  5399.  
  5400.     if (! control.IsNumeric(type))
  5401.     {
  5402.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5403.                       unary_expression -> LeftToken(),
  5404.                       unary_expression -> RightToken(),
  5405.                       type -> Name());
  5406.         unary_expression -> symbol = control.no_type;
  5407.         return unary_expression;
  5408.     }
  5409.  
  5410.     return ((type == control.byte_type || type == control.short_type || type == control.char_type)
  5411.                                                 ? ConvertToType(unary_expression, control.int_type)
  5412.                                                 : unary_expression);
  5413. }
  5414.  
  5415.  
  5416. void Semantic::BinaryNumericPromotion(AstBinaryExpression *binary_expression)
  5417. {
  5418.     AstExpression *left_expr = binary_expression -> left_expression;
  5419.     AstExpression *right_expr = binary_expression -> right_expression;
  5420.  
  5421.     TypeSymbol *left_type  = left_expr -> Type(),
  5422.                *right_type = right_expr -> Type();
  5423.  
  5424.     if (left_type == control.no_type || right_type == control.no_type)
  5425.     {
  5426.         binary_expression -> symbol = control.no_type;
  5427.         return;
  5428.     }
  5429.  
  5430.     if (! control.IsNumeric(left_type))
  5431.     {
  5432.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5433.                       left_expr -> LeftToken(),
  5434.                       left_expr -> RightToken(),
  5435.                       left_type -> Name());
  5436.         binary_expression -> symbol = control.no_type;
  5437.         return;
  5438.     }
  5439.     else if (! control.IsNumeric(right_type))
  5440.     {
  5441.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5442.                       right_expr -> LeftToken(),
  5443.                       right_expr -> RightToken(),
  5444.                       right_type -> Name());
  5445.         binary_expression -> symbol = control.no_type;
  5446.         return;
  5447.     }
  5448.  
  5449.     if (left_type == control.double_type)
  5450.     {
  5451.         if (right_type != control.double_type)
  5452.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.double_type);
  5453.         binary_expression -> symbol = control.double_type;
  5454.     }
  5455.     else if (right_type == control.double_type)
  5456.     {
  5457.         if (left_type != control.double_type)
  5458.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.double_type);
  5459.         binary_expression -> symbol = control.double_type;
  5460.     }
  5461.     else if (left_type == control.float_type)
  5462.     {
  5463.         if (right_type != control.float_type)
  5464.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.float_type);
  5465.         binary_expression -> symbol = control.float_type;
  5466.     }
  5467.     else if (right_type == control.float_type)
  5468.     {
  5469.         if (left_type != control.float_type)
  5470.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.float_type);
  5471.         binary_expression -> symbol = control.float_type;
  5472.     }
  5473.     else if (left_type == control.long_type)
  5474.     {
  5475.         if (right_type != control.long_type)
  5476.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.long_type);
  5477.         binary_expression -> symbol = control.long_type;
  5478.     }
  5479.     else if (right_type == control.long_type)
  5480.     {
  5481.         if (left_type != control.long_type)
  5482.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.long_type);
  5483.         binary_expression -> symbol = control.long_type;
  5484.     }
  5485.     else
  5486.     {
  5487.         if (left_type != control.int_type)
  5488.             binary_expression -> left_expression = ConvertToType(binary_expression -> left_expression, control.int_type);
  5489.         if (right_type != control.int_type)
  5490.             binary_expression -> right_expression = ConvertToType(binary_expression -> right_expression, control.int_type);
  5491.         binary_expression -> symbol = control.int_type;
  5492.     }
  5493.  
  5494.     return;
  5495. }
  5496.  
  5497.  
  5498. void Semantic::BinaryNumericPromotion(AstAssignmentExpression *assignment_expression)
  5499. {
  5500.     AstExpression *left_expr = assignment_expression -> left_hand_side;
  5501.     AstExpression *right_expr = assignment_expression -> expression;
  5502.  
  5503.     TypeSymbol *left_type  = left_expr -> Type(),
  5504.                *right_type = right_expr -> Type();
  5505.  
  5506.     if (left_type == control.no_type || right_type == control.no_type)
  5507.         return;
  5508.  
  5509.     if (! control.IsNumeric(left_type))
  5510.     {
  5511.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5512.                       left_expr -> LeftToken(),
  5513.                       left_expr -> RightToken(),
  5514.                       left_type -> Name());
  5515.         return;
  5516.     }
  5517.     else if (! control.IsNumeric(right_type))
  5518.     {
  5519.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5520.                       right_expr -> LeftToken(),
  5521.                       right_expr -> RightToken(),
  5522.                       right_type -> Name());
  5523.         return;
  5524.     }
  5525.  
  5526.     if (left_type == control.double_type)
  5527.     {
  5528.         if (right_type != control.double_type)
  5529.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.double_type);
  5530.     }
  5531.     else if (right_type == control.double_type)
  5532.     {
  5533.         if (left_type != control.double_type)
  5534.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.double_type);
  5535.     }
  5536.     else if (left_type == control.float_type)
  5537.     {
  5538.         if (right_type != control.float_type)
  5539.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.float_type);
  5540.     }
  5541.     else if (right_type == control.float_type)
  5542.     {
  5543.         if (left_type != control.float_type)
  5544.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.float_type);
  5545.     }
  5546.     else if (left_type == control.long_type)
  5547.     {
  5548.         if (right_type != control.long_type)
  5549.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.long_type);
  5550.     }
  5551.     else if (right_type == control.long_type)
  5552.     {
  5553.         if (left_type != control.long_type)
  5554.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.long_type);
  5555.     }
  5556.     else
  5557.     {
  5558.         if (left_type != control.int_type)
  5559.             assignment_expression -> left_hand_side = ConvertToType(assignment_expression -> left_hand_side, control.int_type);
  5560.         if (right_type != control.int_type)
  5561.             assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.int_type);
  5562.     }
  5563.  
  5564.     return;
  5565. }
  5566.  
  5567.  
  5568. void Semantic::BinaryNumericPromotion(AstConditionalExpression *conditional_expression)
  5569. {
  5570.     AstExpression *left_expr = conditional_expression -> true_expression;
  5571.     AstExpression *right_expr = conditional_expression -> false_expression;
  5572.  
  5573.     TypeSymbol *left_type  = left_expr -> Type(),
  5574.                *right_type = right_expr -> Type();
  5575.  
  5576.     if (left_type == control.no_type || right_type == control.no_type)
  5577.     {
  5578.         conditional_expression -> symbol = control.no_type;
  5579.         return;
  5580.     }
  5581.  
  5582.     if (! control.IsNumeric(left_type))
  5583.     {
  5584.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5585.                       left_expr -> LeftToken(),
  5586.                       left_expr -> RightToken(),
  5587.                       left_type -> Name());
  5588.         conditional_expression -> symbol = control.no_type;
  5589.         return;
  5590.     }
  5591.     else if (! control.IsNumeric(right_type))
  5592.     {
  5593.        ReportSemError(SemanticError::TYPE_NOT_NUMERIC,
  5594.                       right_expr -> LeftToken(),
  5595.                       right_expr -> RightToken(),
  5596.                       right_type -> Name());
  5597.         conditional_expression -> symbol = control.no_type;
  5598.         return;
  5599.     }
  5600.  
  5601.     if (left_type == control.double_type)
  5602.     {
  5603.         if (right_type != control.double_type)
  5604.             conditional_expression -> false_expression =
  5605.                         ConvertToType(conditional_expression -> false_expression, control.double_type);
  5606.         conditional_expression -> symbol = control.double_type;
  5607.     }
  5608.     else if (right_type == control.double_type)
  5609.     {
  5610.         if (left_type != control.double_type)
  5611.             conditional_expression -> true_expression =
  5612.                         ConvertToType(conditional_expression -> true_expression, control.double_type);
  5613.         conditional_expression -> symbol = control.double_type;
  5614.     }
  5615.     else if (left_type == control.float_type)
  5616.     {
  5617.         if (right_type != control.float_type)
  5618.             conditional_expression -> false_expression =
  5619.                         ConvertToType(conditional_expression -> false_expression, control.float_type);
  5620.         conditional_expression -> symbol = control.float_type;
  5621.     }
  5622.     else if (right_type == control.float_type)
  5623.     {
  5624.         if (left_type != control.float_type)
  5625.             conditional_expression -> true_expression =
  5626.                         ConvertToType(conditional_expression -> true_expression, control.float_type);
  5627.         conditional_expression -> symbol = control.float_type;
  5628.     }
  5629.     else if (left_type == control.long_type)
  5630.     {
  5631.         if (right_type != control.long_type)
  5632.             conditional_expression -> false_expression =
  5633.                         ConvertToType(conditional_expression -> false_expression, control.long_type);
  5634.         conditional_expression -> symbol = control.long_type;
  5635.     }
  5636.     else if (right_type == control.long_type)
  5637.     {
  5638.         if (left_type != control.long_type)
  5639.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, control.long_type);
  5640.         conditional_expression -> symbol = control.long_type;
  5641.     }
  5642.     else
  5643.     {
  5644.         if (left_type != control.int_type)
  5645.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, control.int_type);
  5646.         if (right_type != control.int_type)
  5647.             conditional_expression -> false_expression =
  5648.                         ConvertToType(conditional_expression -> false_expression, control.int_type);
  5649.         conditional_expression -> symbol = control.int_type;
  5650.     }
  5651.  
  5652.     return;
  5653. }
  5654.  
  5655.  
  5656. void Semantic::ProcessPLUS(AstBinaryExpression *expr)
  5657. {
  5658.     ProcessExpression(expr -> left_expression);
  5659.     ProcessExpression(expr -> right_expression);
  5660.  
  5661.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5662.                *right_type = expr -> right_expression -> Type();
  5663.  
  5664.     if (left_type == control.no_type || right_type == control.no_type)
  5665.         expr -> symbol = control.no_type;
  5666.     else if (left_type == control.String() || right_type == control.String())
  5667.     {
  5668.         //
  5669.         // TODO: Is this needed ?
  5670.         //
  5671.         // This operation may throw OutOfMemoryError
  5672.         //
  5673.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  5674.         if (exception_set)
  5675.         {
  5676.             exception_set -> AddElement(control.Error());
  5677.         }
  5678.  
  5679.         //
  5680.         // Perform conversion if necessary.
  5681.         //
  5682.         if (expr -> left_expression -> value == control.NullValue())
  5683.         {
  5684.              expr -> left_expression -> value = control.null_literal;
  5685.              expr -> left_expression -> symbol = control.String();
  5686.         }
  5687.         else if (left_type != control.String())
  5688.         {
  5689.             AddStringConversionDependence(left_type, expr -> binary_operator_token);
  5690.             if (left_type == control.void_type)
  5691.                  ReportSemError(SemanticError::VOID_TO_STRING,
  5692.                                 expr -> left_expression -> LeftToken(),
  5693.                                 expr -> left_expression -> RightToken());
  5694.             else expr -> left_expression = ConvertToType(expr -> left_expression, control.String());
  5695.         }
  5696.         else if (expr -> right_expression -> value == control.NullValue())
  5697.         {
  5698.              expr -> right_expression -> value = control.null_literal;
  5699.              expr -> right_expression -> symbol = control.String();
  5700.         }
  5701.         else if (right_type != control.String())
  5702.         {
  5703.             AddStringConversionDependence(right_type, expr -> binary_operator_token);
  5704.             if (right_type == control.void_type)
  5705.                  ReportSemError(SemanticError::VOID_TO_STRING,
  5706.                                 expr -> right_expression -> LeftToken(),
  5707.                                 expr -> right_expression -> RightToken());
  5708.             else expr -> right_expression = ConvertToType(expr -> right_expression, control.String());
  5709.         }
  5710.  
  5711.         AddDependence(ThisType(), control.StringBuffer(), expr -> binary_operator_token);
  5712.  
  5713.         //
  5714.         // If both subexpressions are strings constants, identify the result as
  5715.         // as a string constant, but do not perform the concatenation here. The
  5716.         // reason being that if we have a long expression of the form
  5717.         //
  5718.         //  s1 + s2 + ... + sn
  5719.         //
  5720.         // where each subexpression s(i) is a string constant, we want to perform
  5721.         // one concatenation and enter a single result into the constant pool instead
  5722.         // of n-1 subresults. See CheckStringConstant in lookup.cpp.
  5723.         //
  5724.  
  5725.         expr -> symbol = control.String();
  5726.     }
  5727.     else
  5728.     {
  5729.         BinaryNumericPromotion(expr);
  5730.  
  5731.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5732.         {
  5733.              if (expr -> Type() == control.double_type)
  5734.              {
  5735.                  DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  5736.                  DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  5737.  
  5738.                  expr -> value = control.double_pool.FindOrInsert(left -> value + right -> value);
  5739.              }
  5740.              else if (expr -> Type() == control.float_type)
  5741.              {
  5742.                  FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  5743.                  FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  5744.  
  5745.                  expr -> value = control.float_pool.FindOrInsert(left -> value + right -> value);
  5746.              }
  5747.              else if (expr -> Type() == control.long_type)
  5748.              {
  5749.                  LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5750.                  LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  5751.  
  5752.                  expr -> value = control.long_pool.FindOrInsert(left -> value + right -> value);
  5753.              }
  5754.              else // assert(expr -> Type() == control.int_type)
  5755.              {
  5756.                  IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5757.                  IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5758.  
  5759.                  expr -> value = control.int_pool.FindOrInsert(left -> value + right -> value);
  5760.              }
  5761.         }
  5762.     }
  5763.  
  5764.     return;
  5765. }
  5766.  
  5767.  
  5768. void Semantic::ProcessLEFT_SHIFT(AstBinaryExpression *expr)
  5769. {
  5770.     ProcessExpression(expr -> left_expression);
  5771.     ProcessExpression(expr -> right_expression);
  5772.  
  5773.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5774.                *right_type = expr -> right_expression -> Type();
  5775.  
  5776.     if (left_type == control.no_type || right_type == control.no_type)
  5777.         expr -> symbol = control.no_type;
  5778.     else if (! control.IsIntegral(left_type))
  5779.     {
  5780.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5781.                        expr -> left_expression -> LeftToken(),
  5782.                        expr -> left_expression -> RightToken(),
  5783.                        left_type -> Name());
  5784.         expr -> symbol = control.no_type;
  5785.     }
  5786.     else if (! control.IsIntegral(right_type))
  5787.     {
  5788.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5789.                        expr -> right_expression -> LeftToken(),
  5790.                        expr -> right_expression -> RightToken(),
  5791.                        right_type -> Name());
  5792.         expr -> symbol = control.no_type;
  5793.     }
  5794.     else
  5795.     {
  5796.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5797.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5798.         if (expr -> right_expression -> Type() == control.long_type)
  5799.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5800.         expr -> symbol = expr -> left_expression -> symbol;
  5801.  
  5802.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5803.         {
  5804.             if (expr -> Type() == control.long_type)
  5805.             {
  5806.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5807.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5808.  
  5809.                 expr -> value = control.long_pool.FindOrInsert(left -> value << (right -> value & 0x3F));
  5810.             }
  5811.             else // assert(expr -> Type() == control.int_type)
  5812.             {
  5813.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5814.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5815.  
  5816.                 expr -> value = control.int_pool.FindOrInsert(left -> value << (0x1F & right -> value));
  5817.             }
  5818.         }
  5819.     }
  5820.  
  5821.     return;
  5822. }
  5823.  
  5824.  
  5825. void Semantic::ProcessRIGHT_SHIFT(AstBinaryExpression *expr)
  5826. {
  5827.     ProcessExpression(expr -> left_expression);
  5828.     ProcessExpression(expr -> right_expression);
  5829.  
  5830.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5831.                *right_type = expr -> right_expression -> Type();
  5832.  
  5833.     if (left_type == control.no_type || right_type == control.no_type)
  5834.         expr -> symbol = control.no_type;
  5835.     else if (! control.IsIntegral(left_type))
  5836.     {
  5837.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5838.                        expr -> left_expression -> LeftToken(),
  5839.                        expr -> left_expression -> RightToken(),
  5840.                        left_type -> Name());
  5841.         expr -> symbol = control.no_type;
  5842.     }
  5843.     else if (! control.IsIntegral(right_type))
  5844.     {
  5845.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5846.                        expr -> right_expression -> LeftToken(),
  5847.                        expr -> right_expression -> RightToken(),
  5848.                        right_type -> Name());
  5849.         expr -> symbol = control.no_type;
  5850.     }
  5851.     else
  5852.     {
  5853.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5854.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5855.         if (expr -> right_expression -> Type() == control.long_type)
  5856.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5857.         expr -> symbol = expr -> left_expression -> symbol;
  5858.  
  5859.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5860.         {
  5861.             if (expr -> Type() == control.long_type)
  5862.             {
  5863.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5864.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5865.  
  5866.                 expr -> value = control.long_pool.FindOrInsert(left -> value >> (right -> value & 0x3F));
  5867.             }
  5868.             else // assert(expr -> Type() == control.int_type)
  5869.             {
  5870.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5871.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5872.  
  5873.                 expr -> value = control.int_pool.FindOrInsert(left -> value >> (0x1F & right -> value));
  5874.             }
  5875.         }
  5876.     }
  5877.  
  5878.     return;
  5879. }
  5880.  
  5881.  
  5882. void Semantic::ProcessUNSIGNED_RIGHT_SHIFT(AstBinaryExpression *expr)
  5883. {
  5884.     ProcessExpression(expr -> left_expression);
  5885.     ProcessExpression(expr -> right_expression);
  5886.  
  5887.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5888.                *right_type = expr -> right_expression -> Type();
  5889.  
  5890.     if (left_type == control.no_type || right_type == control.no_type)
  5891.         expr -> symbol = control.no_type;
  5892.     else if (! control.IsIntegral(left_type))
  5893.     {
  5894.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5895.                        expr -> left_expression -> LeftToken(),
  5896.                        expr -> left_expression -> RightToken(),
  5897.                        left_type -> Name());
  5898.         expr -> symbol = control.no_type;
  5899.     }
  5900.     else if (! control.IsIntegral(right_type))
  5901.     {
  5902.         ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  5903.                        expr -> right_expression -> LeftToken(),
  5904.                        expr -> right_expression -> RightToken(),
  5905.                        right_type -> Name());
  5906.         expr -> symbol = control.no_type;
  5907.     }
  5908.     else
  5909.     {
  5910.         expr -> left_expression  = PromoteUnaryNumericExpression(expr -> left_expression);
  5911.         expr -> right_expression = PromoteUnaryNumericExpression(expr -> right_expression);
  5912.         if (expr -> right_expression -> Type() == control.long_type)
  5913.             expr -> right_expression = ConvertToType(expr -> right_expression, control.int_type);
  5914.         expr -> symbol = expr -> left_expression -> symbol;
  5915.  
  5916.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5917.         {
  5918.             if (expr -> Type() == control.long_type)
  5919.             {
  5920.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5921.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5922.                 int right_value = right -> value & 0x3F;
  5923.  
  5924.                 LongInt value = left -> value >> right_value;
  5925.                 if (left -> value < 0)
  5926.                     value += (LongInt(2) << (63 - right_value));
  5927.                 expr -> value = control.long_pool.FindOrInsert(value);
  5928.             }
  5929.             else // assert(expr -> Type() == control.int_type)
  5930.             {
  5931.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5932.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5933.  
  5934.                 int value = left -> value >> (0x1F & right -> value);
  5935.                 if (left -> value < 0)
  5936.                      value += (2 << (31 - (0x1F & right -> value)));
  5937.                 expr -> value = control.int_pool.FindOrInsert(value);
  5938.             }
  5939.         }
  5940.     }
  5941.  
  5942.     return;
  5943. }
  5944.  
  5945.  
  5946. void Semantic::ProcessLESS(AstBinaryExpression *expr)
  5947. {
  5948.     ProcessExpression(expr -> left_expression);
  5949.     ProcessExpression(expr -> right_expression);
  5950.  
  5951.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  5952.                *right_type = expr -> right_expression -> Type();
  5953.  
  5954.     if (left_type != control.no_type && right_type != control.no_type)
  5955.     {
  5956.         BinaryNumericPromotion(expr);
  5957.  
  5958.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  5959.         {
  5960.             if (expr -> Type() == control.double_type)
  5961.             {
  5962.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  5963.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  5964.  
  5965.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5966.             }
  5967.             else if (expr -> Type() == control.float_type)
  5968.             {
  5969.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  5970.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  5971.  
  5972.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5973.             }
  5974.             else if (expr -> Type() == control.long_type)
  5975.            {
  5976.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  5977.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  5978.  
  5979.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5980.             }
  5981.             else // assert(expr -> Type() == control.int_type)
  5982.             {
  5983.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  5984.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  5985.  
  5986.                 expr -> value = control.int_pool.FindOrInsert(left -> value < right -> value ? 1 : 0);
  5987.             }
  5988.         }
  5989.     }
  5990.  
  5991.     expr -> symbol = control.boolean_type;
  5992.  
  5993.     return;
  5994. }
  5995.  
  5996.  
  5997. void Semantic::ProcessGREATER(AstBinaryExpression *expr)
  5998. {
  5999.     ProcessExpression(expr -> left_expression);
  6000.     ProcessExpression(expr -> right_expression);
  6001.  
  6002.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6003.                *right_type = expr -> right_expression -> Type();
  6004.  
  6005.     if (left_type != control.no_type && right_type != control.no_type)
  6006.     {
  6007.         BinaryNumericPromotion(expr);
  6008.  
  6009.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6010.         {
  6011.             if (expr -> Type() == control.double_type)
  6012.             {
  6013.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6014.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6015.  
  6016.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  6017.             }
  6018.             else if (expr -> Type() == control.float_type)
  6019.             {
  6020.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6021.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6022.  
  6023.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  6024.             }
  6025.             else if (expr -> Type() == control.long_type)
  6026.             {
  6027.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6028.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6029.  
  6030.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  6031.             }
  6032.             else // assert(expr -> Type() == control.int_type)
  6033.             {
  6034.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6035.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6036.  
  6037.                 expr -> value = control.int_pool.FindOrInsert(left -> value > right -> value ? 1 : 0);
  6038.             }
  6039.         }
  6040.     }
  6041.  
  6042.     expr -> symbol = control.boolean_type;
  6043.  
  6044.     return;
  6045. }
  6046.  
  6047.  
  6048. void Semantic::ProcessLESS_EQUAL(AstBinaryExpression *expr)
  6049. {
  6050.     ProcessExpression(expr -> left_expression);
  6051.     ProcessExpression(expr -> right_expression);
  6052.  
  6053.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6054.                *right_type = expr -> right_expression -> Type();
  6055.  
  6056.     if (left_type != control.no_type && right_type != control.no_type)
  6057.     {
  6058.         BinaryNumericPromotion(expr);
  6059.  
  6060.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6061.         {
  6062.             if (expr -> Type() == control.double_type)
  6063.             {
  6064.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6065.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6066.  
  6067.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6068.             }
  6069.             else if (expr -> Type() == control.float_type)
  6070.             {
  6071.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6072.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6073.  
  6074.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6075.             }
  6076.             else if (expr -> Type() == control.long_type)
  6077.             {
  6078.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6079.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6080.  
  6081.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6082.             }
  6083.             else // assert(expr -> Type() == control.int_type)
  6084.             {
  6085.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6086.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6087.  
  6088.                 expr -> value = control.int_pool.FindOrInsert(left -> value <= right -> value ? 1 : 0);
  6089.             }
  6090.         }
  6091.     }
  6092.  
  6093.     expr -> symbol = control.boolean_type;
  6094.  
  6095.     return;
  6096. }
  6097.  
  6098.  
  6099. void Semantic::ProcessGREATER_EQUAL(AstBinaryExpression *expr)
  6100. {
  6101.     ProcessExpression(expr -> left_expression);
  6102.     ProcessExpression(expr -> right_expression);
  6103.  
  6104.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6105.                *right_type = expr -> right_expression -> Type();
  6106.  
  6107.     if (left_type != control.no_type && right_type != control.no_type)
  6108.     {
  6109.         BinaryNumericPromotion(expr);
  6110.  
  6111.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6112.         {
  6113.             if (expr -> Type() == control.double_type)
  6114.             {
  6115.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6116.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6117.  
  6118.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6119.             }
  6120.             else if (expr -> Type() == control.float_type)
  6121.             {
  6122.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6123.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6124.  
  6125.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6126.             }
  6127.             else if (expr -> Type() == control.long_type)
  6128.             {
  6129.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6130.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6131.  
  6132.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6133.             }
  6134.             else // assert(expr -> Type() == control.int_type)
  6135.             {
  6136.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6137.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6138.  
  6139.                 expr -> value = control.int_pool.FindOrInsert(left -> value >= right -> value ? 1 : 0);
  6140.             }
  6141.         }
  6142.     }
  6143.  
  6144.     expr -> symbol = control.boolean_type;
  6145.  
  6146.     return;
  6147. }
  6148.  
  6149.  
  6150. void Semantic::ProcessAND(AstBinaryExpression *expr)
  6151. {
  6152.     ProcessExpression(expr -> left_expression);
  6153.     ProcessExpression(expr -> right_expression);
  6154.  
  6155.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6156.                *right_type = expr -> right_expression -> Type();
  6157.  
  6158.     if (left_type == control.no_type || right_type == control.no_type)
  6159.         expr -> symbol = control.no_type;
  6160.     else
  6161.     {
  6162.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6163.              expr -> symbol = control.boolean_type;
  6164.         else
  6165.         {
  6166.             BinaryNumericPromotion(expr);
  6167.  
  6168.             TypeSymbol *expr_type = expr -> Type();
  6169.  
  6170.             if (expr_type != control.no_type)
  6171.             {
  6172.                 if (! control.IsIntegral(expr_type))
  6173.                 {
  6174.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6175.                                    expr -> LeftToken(),
  6176.                                    expr -> RightToken(),
  6177.                                    expr_type -> Name());
  6178.                     expr -> symbol = control.no_type;
  6179.                 }
  6180.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6181.                 {
  6182.                     if (expr_type == control.long_type)
  6183.                     {
  6184.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6185.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6186.  
  6187.                         expr -> value = control.long_pool.FindOrInsert(left -> value & right -> value);
  6188.                     }
  6189.                     else // assert(expr_type == control.int_type)
  6190.                     {
  6191.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6192.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6193.  
  6194.                         expr -> value = control.int_pool.FindOrInsert(left -> value & right -> value);
  6195.                     }
  6196.                 }
  6197.             }
  6198.         }
  6199.     }
  6200.  
  6201.     return;
  6202. }
  6203.  
  6204.  
  6205. void Semantic::ProcessXOR(AstBinaryExpression *expr)
  6206. {
  6207.     ProcessExpression(expr -> left_expression);
  6208.     ProcessExpression(expr -> right_expression);
  6209.  
  6210.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6211.                *right_type = expr -> right_expression -> Type();
  6212.  
  6213.     if (left_type == control.no_type || right_type == control.no_type)
  6214.         expr -> symbol = control.no_type;
  6215.     else
  6216.     {
  6217.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6218.              expr -> symbol = control.boolean_type;
  6219.         else
  6220.         {
  6221.             BinaryNumericPromotion(expr);
  6222.  
  6223.             TypeSymbol *expr_type = expr -> Type();
  6224.  
  6225.             if (expr_type != control.no_type)
  6226.             {
  6227.                 if (! control.IsIntegral(expr_type))
  6228.                 {
  6229.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6230.                                    expr -> LeftToken(),
  6231.                                    expr -> RightToken(),
  6232.                                    expr_type -> Name());
  6233.                     expr -> symbol = control.no_type;
  6234.                 }
  6235.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6236.                 {
  6237.                     if (expr_type == control.long_type)
  6238.                     {
  6239.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6240.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6241.  
  6242.                         expr -> value = control.long_pool.FindOrInsert(left -> value ^ right -> value);
  6243.                     }
  6244.                     else // assert(expr_type == control.int_type)
  6245.                     {
  6246.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6247.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6248.  
  6249.                         expr -> value = control.int_pool.FindOrInsert(left -> value ^ right -> value);
  6250.                     }
  6251.                 }
  6252.             }
  6253.         }
  6254.     }
  6255.  
  6256.     return;
  6257. }
  6258.  
  6259.  
  6260. void Semantic::ProcessIOR(AstBinaryExpression *expr)
  6261. {
  6262.     ProcessExpression(expr -> left_expression);
  6263.     ProcessExpression(expr -> right_expression);
  6264.  
  6265.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6266.                *right_type = expr -> right_expression -> Type();
  6267.  
  6268.     if (left_type == control.no_type || right_type == control.no_type)
  6269.         expr -> symbol = control.no_type;
  6270.     else
  6271.     {
  6272.         if (left_type == control.boolean_type && right_type == control.boolean_type)
  6273.              expr -> symbol = control.boolean_type;
  6274.         else
  6275.         {
  6276.             BinaryNumericPromotion(expr);
  6277.  
  6278.             TypeSymbol *expr_type = expr -> Type();
  6279.  
  6280.             if (expr_type != control.no_type)
  6281.             {
  6282.                 if (! control.IsIntegral(expr_type))
  6283.                 {
  6284.                     ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  6285.                                    expr -> LeftToken(),
  6286.                                    expr -> RightToken(),
  6287.                                    expr_type -> Name());
  6288.                     expr -> symbol = control.no_type;
  6289.                 }
  6290.                 else if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6291.                 {
  6292.                     if (expr_type == control.long_type)
  6293.                     {
  6294.                         LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6295.                         LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6296.  
  6297.                         expr -> value = control.long_pool.FindOrInsert(left -> value | right -> value);
  6298.                     }
  6299.                     else // assert(expr_type == control.int_type)
  6300.                     {
  6301.                         IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6302.                         IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6303.  
  6304.                         expr -> value = control.int_pool.FindOrInsert(left -> value | right -> value);
  6305.                     }
  6306.                 }
  6307.             }
  6308.         }
  6309.     }
  6310.  
  6311.     return;
  6312. }
  6313.  
  6314.  
  6315. void Semantic::ProcessAND_AND(AstBinaryExpression *expr)
  6316. {
  6317.     ProcessExpression(expr -> left_expression);
  6318.     ProcessExpression(expr -> right_expression);
  6319.  
  6320.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6321.                *right_type = expr -> right_expression -> Type();
  6322.  
  6323.     if (left_type != control.no_type && right_type != control.no_type)
  6324.     {
  6325.         if (left_type != control.boolean_type)
  6326.         {
  6327.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6328.                            expr -> left_expression -> LeftToken(),
  6329.                            expr -> left_expression -> RightToken(),
  6330.                            left_type -> Name());
  6331.         }
  6332.         else if (right_type != control.boolean_type)
  6333.         {
  6334.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6335.                            expr -> right_expression -> LeftToken(),
  6336.                            expr -> right_expression -> RightToken(),
  6337.                            right_type -> Name());
  6338.         }
  6339.         else if (expr -> left_expression -> IsConstant())
  6340.         {
  6341.             IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6342.             if (! left -> value)
  6343.                 expr -> value = control.int_pool.FindOrInsert(0);
  6344.             else if (expr -> right_expression -> IsConstant())
  6345.             {
  6346.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6347.                 expr -> value = control.int_pool.FindOrInsert(left -> value && right -> value ? 1 : 0);
  6348.             }
  6349.         }
  6350.     }
  6351.  
  6352.     expr -> symbol = control.boolean_type;
  6353.  
  6354.     return;
  6355. }
  6356.  
  6357.  
  6358. void Semantic::ProcessOR_OR(AstBinaryExpression *expr)
  6359. {
  6360.     ProcessExpression(expr -> left_expression);
  6361.     ProcessExpression(expr -> right_expression);
  6362.  
  6363.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6364.                *right_type = expr -> right_expression -> Type();
  6365.  
  6366.     if (left_type != control.no_type && right_type != control.no_type)
  6367.     {
  6368.         if (left_type != control.boolean_type)
  6369.         {
  6370.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6371.                            expr -> left_expression -> LeftToken(),
  6372.                            expr -> left_expression -> RightToken(),
  6373.                            left_type -> Name());
  6374.         }
  6375.         else if (right_type != control.boolean_type)
  6376.         {
  6377.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6378.                            expr -> right_expression -> LeftToken(),
  6379.                            expr -> right_expression -> RightToken(),
  6380.                            right_type -> Name());
  6381.         }
  6382.         else if (expr -> left_expression -> IsConstant())
  6383.         {
  6384.             IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6385.             if (left -> value)
  6386.                 expr -> value = control.int_pool.FindOrInsert(1);
  6387.             else if (expr -> right_expression -> IsConstant())
  6388.             {
  6389.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6390.                 expr -> value = control.int_pool.FindOrInsert(left -> value || right -> value ? 1 : 0);
  6391.             }
  6392.         }
  6393.     }
  6394.  
  6395.     expr -> symbol = control.boolean_type;
  6396.  
  6397.     return;
  6398. }
  6399.  
  6400.  
  6401. void Semantic::ProcessEQUAL_EQUAL(AstBinaryExpression *expr)
  6402. {
  6403.     ProcessExpressionOrStringConstant(expr -> left_expression);
  6404.     ProcessExpressionOrStringConstant(expr -> right_expression);
  6405.  
  6406.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6407.                *right_type = expr -> right_expression -> Type();
  6408.  
  6409.     if (left_type != control.no_type && right_type != control.no_type)
  6410.     {
  6411.         if (left_type != right_type)
  6412.         {
  6413.             if (left_type -> Primitive() && right_type -> Primitive())
  6414.                  BinaryNumericPromotion(expr);
  6415.             else if (CanCastConvert(left_type, right_type, expr -> binary_operator_token))
  6416.                  expr -> right_expression = ConvertToType(expr -> right_expression, left_type);
  6417.             else if (CanCastConvert(right_type, left_type, expr -> binary_operator_token))
  6418.                  expr -> left_expression = ConvertToType(expr -> left_expression, right_type);
  6419.             else
  6420.             {
  6421.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
  6422.                                expr -> LeftToken(),
  6423.                                expr -> RightToken(),
  6424.                                expr -> left_expression -> Type() -> ContainingPackage() -> PackageName(),
  6425.                                expr -> left_expression -> Type() -> ExternalName(),
  6426.                                expr -> right_expression -> Type() -> ContainingPackage() -> PackageName(),
  6427.                                expr -> right_expression -> Type() -> ExternalName());
  6428.             }
  6429.         }
  6430.         else
  6431.         {
  6432.             if (left_type == control.void_type)
  6433.                 ReportSemError(SemanticError::VOID_TYPE_IN_EQUALITY_EXPRESSION,
  6434.                                expr -> LeftToken(),
  6435.                                expr -> RightToken(),
  6436.                                expr -> left_expression -> Type() -> Name(),
  6437.                                expr -> right_expression -> Type() -> Name());
  6438.             expr -> symbol = left_type;
  6439.         }
  6440.  
  6441.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6442.         {
  6443.             LiteralValue *left = expr -> left_expression -> value;
  6444.             LiteralValue *right = expr -> right_expression -> value;
  6445.  
  6446.             if (expr -> Type() == control.double_type)
  6447.             {
  6448.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6449.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6450.  
  6451.                 expr -> value = control.int_pool.FindOrInsert(left -> value == right -> value ? 1 : 0);
  6452.             }
  6453.             else if (expr -> Type() == control.float_type)
  6454.             {
  6455.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6456.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6457.  
  6458.                 expr -> value = control.int_pool.FindOrInsert(left -> value == right -> value ? 1 : 0);
  6459.             }
  6460.             else expr -> value = control.int_pool.FindOrInsert(left == right ? 1 : 0);
  6461.         }
  6462.     }
  6463.  
  6464.     expr -> symbol = control.boolean_type;
  6465.  
  6466.     return;
  6467. }
  6468.  
  6469.  
  6470. void Semantic::ProcessNOT_EQUAL(AstBinaryExpression *expr)
  6471. {
  6472.     ProcessExpressionOrStringConstant(expr -> left_expression);
  6473.     ProcessExpressionOrStringConstant(expr -> right_expression);
  6474.  
  6475.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6476.                *right_type = expr -> right_expression -> Type();
  6477.  
  6478.     if (left_type != control.no_type && right_type != control.no_type)
  6479.     {
  6480.         if (left_type != right_type)
  6481.         {
  6482.             if (left_type -> Primitive() && right_type -> Primitive())
  6483.                  BinaryNumericPromotion(expr);
  6484.             else if (CanCastConvert(left_type, right_type, expr -> binary_operator_token))
  6485.                  expr -> right_expression = ConvertToType(expr -> right_expression, left_type);
  6486.             else if (CanCastConvert(right_type, left_type, expr -> binary_operator_token))
  6487.                  expr -> left_expression = ConvertToType(expr -> left_expression, right_type);
  6488.             else
  6489.             {
  6490.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_BINARY_EXPRESSION,
  6491.                                expr -> LeftToken(),
  6492.                                expr -> RightToken(),
  6493.                                expr -> left_expression -> Type() -> ContainingPackage() -> PackageName(),
  6494.                                expr -> left_expression -> Type() -> ExternalName(),
  6495.                                expr -> right_expression -> Type() -> ContainingPackage() -> PackageName(),
  6496.                                expr -> right_expression -> Type() -> ExternalName());
  6497.             }
  6498.         }
  6499.         else
  6500.         {
  6501.             if (left_type == control.void_type)
  6502.                 ReportSemError(SemanticError::VOID_TYPE_IN_EQUALITY_EXPRESSION,
  6503.                                expr -> LeftToken(),
  6504.                                expr -> RightToken());
  6505.             expr -> symbol = left_type;
  6506.         }
  6507.  
  6508.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6509.         {
  6510.             LiteralValue *left = expr -> left_expression -> value;
  6511.             LiteralValue *right = expr -> right_expression -> value;
  6512.  
  6513.             if (expr -> Type() == control.double_type)
  6514.             {
  6515.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6516.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6517.  
  6518.                 expr -> value = control.int_pool.FindOrInsert(left -> value != right -> value ? 1 : 0);
  6519.             }
  6520.             else if (expr -> Type() == control.float_type)
  6521.             {
  6522.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6523.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6524.  
  6525.                 expr -> value = control.int_pool.FindOrInsert(left -> value != right -> value ? 1 : 0);
  6526.             }
  6527.             else expr -> value = control.int_pool.FindOrInsert(left != right ? 1 : 0);
  6528.         }
  6529.     }
  6530.  
  6531.     expr -> symbol = control.boolean_type;
  6532.  
  6533.     return;
  6534. }
  6535.  
  6536.  
  6537. void Semantic::ProcessSTAR(AstBinaryExpression *expr)
  6538. {
  6539.     ProcessExpression(expr -> left_expression);
  6540.     ProcessExpression(expr -> right_expression);
  6541.  
  6542.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6543.                *right_type = expr -> right_expression -> Type();
  6544.  
  6545.     if (left_type == control.no_type || right_type == control.no_type)
  6546.         expr -> symbol = control.no_type;
  6547.     else
  6548.     {
  6549.         BinaryNumericPromotion(expr);
  6550.  
  6551.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6552.         {
  6553.             if (expr -> Type() == control.double_type)
  6554.             {
  6555.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6556.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6557.  
  6558.                 expr -> value = control.double_pool.FindOrInsert(left -> value * right -> value);
  6559.             }
  6560.             else if (expr -> Type() == control.float_type)
  6561.             {
  6562.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6563.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6564.  
  6565.                 expr -> value = control.float_pool.FindOrInsert(left -> value * right -> value);
  6566.             }
  6567.             else if (expr -> Type() == control.long_type)
  6568.             {
  6569.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6570.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6571.  
  6572.                 expr -> value = control.long_pool.FindOrInsert(left -> value * right -> value);
  6573.             }
  6574.             else if (expr -> Type() == control.int_type)
  6575.             {
  6576.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6577.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6578.  
  6579.                 expr -> value = control.int_pool.FindOrInsert(left -> value * right -> value);
  6580.             }
  6581.         }
  6582.     }
  6583.  
  6584.     return;
  6585. }
  6586.  
  6587.  
  6588. void Semantic::ProcessMINUS(AstBinaryExpression *expr)
  6589. {
  6590.     ProcessExpression(expr -> left_expression);
  6591.     ProcessExpression(expr -> right_expression);
  6592.  
  6593.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6594.                *right_type = expr -> right_expression -> Type();
  6595.  
  6596.     if (left_type == control.no_type || right_type == control.no_type)
  6597.         expr -> symbol = control.no_type;
  6598.     else
  6599.     {
  6600.         BinaryNumericPromotion(expr);
  6601.  
  6602.         if (expr -> left_expression -> IsConstant() && expr -> right_expression -> IsConstant())
  6603.         {
  6604.             if (expr -> Type() == control.double_type)
  6605.             {
  6606.                 DoubleLiteralValue *left = (DoubleLiteralValue *) expr -> left_expression -> value;
  6607.                 DoubleLiteralValue *right = (DoubleLiteralValue *) expr -> right_expression -> value;
  6608.  
  6609.                 expr -> value = control.double_pool.FindOrInsert(left -> value - right -> value);
  6610.             }
  6611.             else if (expr -> Type() == control.float_type)
  6612.             {
  6613.                 FloatLiteralValue *left = (FloatLiteralValue *) expr -> left_expression -> value;
  6614.                 FloatLiteralValue *right = (FloatLiteralValue *) expr -> right_expression -> value;
  6615.  
  6616.                 expr -> value = control.float_pool.FindOrInsert(left -> value - right -> value);
  6617.             }
  6618.             else if (expr -> Type() == control.long_type)
  6619.             {
  6620.                 LongLiteralValue *left = (LongLiteralValue *) expr -> left_expression -> value;
  6621.                 LongLiteralValue *right = (LongLiteralValue *) expr -> right_expression -> value;
  6622.  
  6623.                 expr -> value = control.long_pool.FindOrInsert(left -> value - right -> value);
  6624.             }
  6625.             else if (expr -> Type() == control.int_type)
  6626.             {
  6627.                 IntLiteralValue *left = (IntLiteralValue *) expr -> left_expression -> value;
  6628.                 IntLiteralValue *right = (IntLiteralValue *) expr -> right_expression -> value;
  6629.  
  6630.                 expr -> value = control.int_pool.FindOrInsert(left -> value - right -> value);
  6631.             }
  6632.         }
  6633.     }
  6634.  
  6635.     return;
  6636. }
  6637.  
  6638.  
  6639. void Semantic::ProcessSLASH(AstBinaryExpression *expr)
  6640. {
  6641.     ProcessExpression(expr -> left_expression);
  6642.     ProcessExpression(expr -> right_expression);
  6643.  
  6644.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6645.                *right_type = expr -> right_expression -> Type();
  6646.  
  6647.     if (left_type == control.no_type || right_type == control.no_type)
  6648.         expr -> symbol = control.no_type;
  6649.     else
  6650.     {
  6651.         BinaryNumericPromotion(expr);
  6652.  
  6653.         //
  6654.         // TODO: Is this needed ?
  6655.         //
  6656.         // This operation may throw ArithmeticException
  6657.         //
  6658.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  6659.         if (exception_set)
  6660.         {
  6661.             exception_set -> AddElement(control.RuntimeException());
  6662.         }
  6663.  
  6664.         AstExpression *left_expression = expr -> left_expression,
  6665.                       *right_expression = expr -> right_expression;
  6666.         if (right_expression -> IsConstant())
  6667.         {
  6668.             //
  6669.             // If the type of the expression is int or long and the right-hand side is 0
  6670.             // then issue an error message.
  6671.             // Otherwise, if both subexpressions are constant, calculate result.
  6672.             //
  6673.             if ((expr -> Type() == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  6674.                 (expr -> Type() == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  6675.             {
  6676.                 ReportSemError(left_expression -> IsConstant() ? SemanticError::ZERO_DIVIDE_ERROR
  6677.                                                                : SemanticError::ZERO_DIVIDE_CAUTION,
  6678.                                expr -> LeftToken(),
  6679.                                expr -> RightToken());
  6680.             }
  6681.             else if (left_expression -> IsConstant())
  6682.             {
  6683.                 if (expr -> Type() == control.double_type)
  6684.                 {
  6685.                     DoubleLiteralValue *left = (DoubleLiteralValue *) left_expression -> value;
  6686.                     DoubleLiteralValue *right = (DoubleLiteralValue *) right_expression -> value;
  6687.  
  6688.                     expr -> value = control.double_pool.FindOrInsert(left -> value / right -> value);
  6689.                 }
  6690.                 else if (expr -> Type() == control.float_type)
  6691.                 {
  6692.                     FloatLiteralValue *left = (FloatLiteralValue *) left_expression -> value;
  6693.                     FloatLiteralValue *right = (FloatLiteralValue *) right_expression -> value;
  6694.  
  6695.                     expr -> value = control.float_pool.FindOrInsert(left -> value / right -> value);
  6696.                 }
  6697.                 else if (expr -> Type() == control.long_type)
  6698.                 {
  6699.                     LongLiteralValue *left = (LongLiteralValue *) left_expression -> value;
  6700.                     LongLiteralValue *right = (LongLiteralValue *) right_expression -> value;
  6701.  
  6702.                     expr -> value = control.long_pool.FindOrInsert(left -> value / right -> value);
  6703.                 }
  6704.                 else if (expr -> Type() == control.int_type)
  6705.                 {
  6706.                     IntLiteralValue *left = (IntLiteralValue *) left_expression -> value;
  6707.                     IntLiteralValue *right = (IntLiteralValue *) right_expression -> value;
  6708.  
  6709.                     //
  6710.                     // There is a bug in the intel hardware where if one tries to compute ((2**32-1) / -1),
  6711.                     // he gets a ZeroDivide exception. Thus, instead of using the straightforward code below,
  6712.                     // we use the short-circuited one that follows:
  6713.                     //
  6714.                     //  expr -> value = control.int_pool.FindOrInsert(left -> value / right -> value);
  6715.                     //
  6716.                     expr -> value = control.int_pool.FindOrInsert(right -> value == -1 ? -(left -> value)
  6717.                                                                                        : left -> value / right -> value);
  6718.                 }
  6719.             }
  6720.         }
  6721.     }
  6722.  
  6723.     return;
  6724. }
  6725.  
  6726.  
  6727. void Semantic::ProcessMOD(AstBinaryExpression *expr)
  6728. {
  6729.     ProcessExpression(expr -> left_expression);
  6730.     ProcessExpression(expr -> right_expression);
  6731.  
  6732.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6733.                *right_type = expr -> right_expression -> Type();
  6734.  
  6735.     if (left_type == control.no_type || right_type == control.no_type)
  6736.         expr -> symbol = control.no_type;
  6737.     else
  6738.     {
  6739.         BinaryNumericPromotion(expr);
  6740.  
  6741.         //
  6742.         // TODO: Is this needed ?
  6743.         //
  6744.         // This operation may throw ArithmeticException
  6745.         //
  6746.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  6747.         if (exception_set)
  6748.         {
  6749.             exception_set -> AddElement(control.RuntimeException());
  6750.         }
  6751.  
  6752.         AstExpression *left_expression = expr -> left_expression,
  6753.                       *right_expression = expr -> right_expression;
  6754.         if (right_expression -> IsConstant())
  6755.         {
  6756.             //
  6757.             // If the type of the expression is int or long and the right-hand side is 0
  6758.             // then issue an error message.
  6759.             // Otherwise, if both subexpressions are constant, calculate result.
  6760.             //
  6761.             if ((expr -> Type() == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  6762.                 (expr -> Type() == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  6763.             {
  6764.                 ReportSemError(left_expression -> IsConstant() ? SemanticError::ZERO_DIVIDE_ERROR
  6765.                                                                : SemanticError::ZERO_DIVIDE_CAUTION,
  6766.                                expr -> LeftToken(),
  6767.                                expr -> RightToken());
  6768.             }
  6769.             else if (left_expression -> IsConstant())
  6770.             {
  6771.                 if (expr -> Type() == control.double_type)
  6772.                 {
  6773.                     DoubleLiteralValue *left = (DoubleLiteralValue *) left_expression -> value;
  6774.                     DoubleLiteralValue *right = (DoubleLiteralValue *) right_expression -> value;
  6775.                     IEEEdouble result = IEEEdouble((u4) 0);
  6776.                     IEEEdouble::Fmodulus(left -> value, right -> value, result);
  6777.  
  6778.                     expr -> value = control.double_pool.FindOrInsert(result);
  6779.                 }
  6780.                 else if (expr -> Type() == control.float_type)
  6781.                 {
  6782.                     FloatLiteralValue *left = (FloatLiteralValue *) left_expression -> value;
  6783.                     FloatLiteralValue *right = (FloatLiteralValue *) right_expression -> value;
  6784.                     IEEEfloat result = IEEEfloat(0);
  6785.                     IEEEfloat::Fmodulus(left -> value, right -> value, result);
  6786.  
  6787.                     expr -> value = control.float_pool.FindOrInsert(result);
  6788.                 }
  6789.                 else if (expr -> Type() == control.long_type)
  6790.                 {
  6791.                     LongLiteralValue *left = (LongLiteralValue *) left_expression -> value;
  6792.                     LongLiteralValue *right = (LongLiteralValue *) right_expression -> value;
  6793.  
  6794.                     expr -> value = control.long_pool.FindOrInsert(left -> value % right -> value);
  6795.                 }
  6796.                 else if (expr -> Type() == control.int_type)
  6797.                 {
  6798.                     IntLiteralValue *left = (IntLiteralValue *) left_expression -> value;
  6799.                     IntLiteralValue *right = (IntLiteralValue *) right_expression -> value;
  6800.  
  6801.                     //
  6802.                     // There is a bug in the intel hardware where if one tries to compute ((2**32-1) / -1),
  6803.                     // he gets a ZeroDivide exception. Thus, instead of using the straightforward code below,
  6804.                     // we use the short-circuited one that follows:
  6805.                     //
  6806.                     // expr -> value = control.int_pool.FindOrInsert(left -> value % right -> value);
  6807.                     //
  6808.                     expr -> value = control.int_pool.FindOrInsert((left -> value  == (signed) 0x80000000 &&
  6809.                                                                    right -> value == (signed) 0xffffffff)
  6810.                                                                                    ? 0
  6811.                                                                                    : left -> value % right -> value);
  6812.                 }
  6813.             }
  6814.         }
  6815.     }
  6816.  
  6817.     return;
  6818. }
  6819.  
  6820.  
  6821. void Semantic::ProcessINSTANCEOF(AstBinaryExpression *expr)
  6822. {
  6823.     ProcessExpression(expr -> left_expression);
  6824.     ProcessExpression(expr -> right_expression);
  6825.  
  6826.     TypeSymbol *left_type  = expr -> left_expression -> Type(),
  6827.                *right_type = expr -> right_expression -> Type();
  6828.  
  6829.     if (left_type -> Primitive())
  6830.     {
  6831.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  6832.                        expr -> left_expression -> LeftToken(),
  6833.                        expr -> left_expression -> RightToken(),
  6834.                        expr -> left_expression -> Type() -> Name());
  6835.     }
  6836.     else if (! CanCastConvert(right_type, left_type, expr -> binary_operator_token)) // can left_type (source) be cast into right_type
  6837.     {
  6838.         ReportSemError(SemanticError::INVALID_INSTANCEOF_CONVERSION,
  6839.                        expr -> LeftToken(),
  6840.                        expr -> RightToken(),
  6841.                        left_type -> ContainingPackage() -> PackageName(),
  6842.                        left_type -> ExternalName(),
  6843.                        right_type -> ContainingPackage() -> PackageName(),
  6844.                        right_type -> ExternalName());
  6845.     }
  6846.  
  6847.     expr -> symbol = control.boolean_type;
  6848.  
  6849.     return;
  6850. }
  6851.  
  6852.  
  6853. void Semantic::ProcessBinaryExpression(Ast *expr)
  6854. {
  6855.     AstBinaryExpression *binary_expression = (AstBinaryExpression *) expr;
  6856.     (this ->* ProcessBinaryExpr[binary_expression -> binary_tag])(binary_expression);
  6857.  
  6858.     return;
  6859. }
  6860.  
  6861.  
  6862. void Semantic::ProcessTypeExpression(Ast *expr)
  6863. {
  6864.     AstTypeExpression *type_expression = (AstTypeExpression *) expr;
  6865.  
  6866.     AstArrayType *array_type = type_expression -> type -> ArrayTypeCast();
  6867.     Ast *actual_type = (array_type ? array_type -> type : type_expression -> type);
  6868.  
  6869.     AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  6870.     TypeSymbol *type = (primitive_type ? FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  6871.  
  6872.     if (array_type)
  6873.         type = type -> GetArrayType((Semantic *) this, array_type -> NumBrackets());
  6874.  
  6875.     type_expression -> symbol = type;
  6876.  
  6877.     return;
  6878. }
  6879.  
  6880.  
  6881. void Semantic::ProcessConditionalExpression(Ast *expr)
  6882. {
  6883.     AstConditionalExpression *conditional_expression = (AstConditionalExpression *) expr;
  6884.  
  6885.     ProcessExpression(conditional_expression -> test_expression);
  6886.     ProcessExpressionOrStringConstant(conditional_expression -> true_expression);
  6887.     ProcessExpressionOrStringConstant(conditional_expression -> false_expression);
  6888.  
  6889.     TypeSymbol *test_type  = conditional_expression -> test_expression -> Type();
  6890.     TypeSymbol *true_type  = conditional_expression -> true_expression -> Type();
  6891.     TypeSymbol *false_type = conditional_expression -> false_expression -> Type();
  6892.  
  6893.     if (test_type == control.no_type || true_type == control.no_type || false_type == control.no_type)
  6894.         conditional_expression -> symbol = control.no_type;
  6895.     else if (test_type != control.boolean_type)
  6896.     {
  6897.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  6898.                        conditional_expression -> test_expression -> LeftToken(),
  6899.                        conditional_expression -> test_expression -> RightToken(),
  6900.                        conditional_expression -> test_expression -> Type() -> Name());
  6901.         conditional_expression -> symbol = control.no_type;
  6902.     }
  6903.     else if (true_type == control.void_type)
  6904.     {
  6905.         ReportSemError(SemanticError::TYPE_IS_VOID,
  6906.                        conditional_expression -> true_expression -> LeftToken(),
  6907.                        conditional_expression -> true_expression -> RightToken(),
  6908.                        conditional_expression -> true_expression -> Type() -> Name());
  6909.         conditional_expression -> symbol = control.no_type;
  6910.     }
  6911.     else if (false_type == control.void_type)
  6912.     {
  6913.         ReportSemError(SemanticError::TYPE_IS_VOID,
  6914.                        conditional_expression -> false_expression -> LeftToken(),
  6915.                        conditional_expression -> false_expression -> RightToken(),
  6916.                        conditional_expression -> false_expression -> Type() -> Name());
  6917.         conditional_expression -> symbol = control.no_type;
  6918.     }
  6919.     else if (true_type -> Primitive())
  6920.     {
  6921.         if (! false_type -> Primitive() ||
  6922.             (true_type != false_type && (true_type == control.boolean_type || false_type == control.boolean_type)))
  6923.         {
  6924.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  6925.                            conditional_expression -> false_expression -> LeftToken(),
  6926.                            conditional_expression -> false_expression -> RightToken(),
  6927.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  6928.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  6929.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  6930.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  6931.             conditional_expression -> symbol = control.no_type;
  6932.         }
  6933.         else // must be a numeric type
  6934.         {
  6935.             if (true_type == false_type)
  6936.                 conditional_expression -> symbol = true_type;
  6937.             else // must be a numeric type
  6938.             {
  6939.                 if (true_type == control.byte_type && false_type == control.short_type)
  6940.                 {
  6941.                     conditional_expression -> true_expression =
  6942.                                 ConvertToType(conditional_expression -> true_expression, control.short_type);
  6943.                     conditional_expression -> symbol = control.short_type;
  6944.                 }
  6945.                 else if (true_type == control.short_type && false_type == control.byte_type)
  6946.                 {
  6947.                     conditional_expression -> false_expression =
  6948.                         ConvertToType(conditional_expression -> false_expression, control.short_type);
  6949.                     conditional_expression -> symbol = control.short_type;
  6950.                 }
  6951.                 else if (IsIntValueRepresentableInType(conditional_expression -> false_expression, true_type))
  6952.                 {
  6953.                     conditional_expression -> false_expression =
  6954.                         ConvertToType(conditional_expression -> false_expression, true_type);
  6955.                     conditional_expression -> symbol = true_type;
  6956.                 }
  6957.                 else if (IsIntValueRepresentableInType(conditional_expression -> true_expression, false_type))
  6958.                 {
  6959.                     conditional_expression -> true_expression =
  6960.                          ConvertToType(conditional_expression -> true_expression, false_type);
  6961.                     conditional_expression -> symbol = false_type;
  6962.                 }
  6963.                 else BinaryNumericPromotion(conditional_expression);
  6964.             }
  6965.  
  6966.             //
  6967.             // If all the relevant subexpressions are constants, compute the results and
  6968.             // set the value of the expression accordingly.
  6969.             //
  6970.             if (conditional_expression -> test_expression -> IsConstant())
  6971.             {
  6972.                 IntLiteralValue *test = (IntLiteralValue *) conditional_expression -> test_expression -> value;
  6973.  
  6974.                 if (test -> value && conditional_expression -> true_expression -> IsConstant())
  6975.                      conditional_expression -> value = conditional_expression -> true_expression -> value;
  6976.                 else if ((! test -> value) && conditional_expression -> false_expression -> IsConstant())
  6977.                      conditional_expression -> value = conditional_expression -> false_expression -> value;
  6978.             }
  6979.         }
  6980.     }
  6981.     else
  6982.     {
  6983.         if (true_type == false_type)
  6984.             conditional_expression -> symbol = true_type;
  6985.         else if (false_type -> Primitive())
  6986.         {
  6987.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  6988.                            conditional_expression -> false_expression -> LeftToken(),
  6989.                            conditional_expression -> false_expression -> RightToken(),
  6990.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  6991.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  6992.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  6993.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  6994.             conditional_expression -> symbol = control.no_type;
  6995.         }
  6996.         else if (true_type == control.null_type)
  6997.             conditional_expression -> symbol = false_type;
  6998.         else if (false_type == control.null_type)
  6999.             conditional_expression -> symbol = true_type;
  7000.         else if (CanAssignmentConvert(false_type, conditional_expression -> true_expression))
  7001.         {
  7002.             conditional_expression -> true_expression = ConvertToType(conditional_expression -> true_expression, false_type);
  7003.             conditional_expression -> symbol = false_type;
  7004.         }
  7005.         else if (CanAssignmentConvert(true_type, conditional_expression -> false_expression))
  7006.         {
  7007.             conditional_expression -> false_expression = ConvertToType(conditional_expression -> false_expression, true_type);
  7008.             conditional_expression -> symbol = true_type;
  7009.         }
  7010.         else
  7011.         {
  7012.             ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_CONDITIONAL_EXPRESSION,
  7013.                            conditional_expression -> false_expression -> LeftToken(),
  7014.                            conditional_expression -> false_expression -> RightToken(),
  7015.                            conditional_expression -> false_expression -> Type() -> ContainingPackage() -> PackageName(),
  7016.                            conditional_expression -> false_expression -> Type() -> ExternalName(),
  7017.                            conditional_expression -> true_expression -> Type() -> ContainingPackage() -> PackageName(),
  7018.                            conditional_expression -> true_expression -> Type() -> ExternalName());
  7019.             conditional_expression -> symbol = control.no_type;
  7020.         }
  7021.     }
  7022.  
  7023.     return;
  7024. }
  7025.  
  7026.  
  7027. void Semantic::ProcessAssignmentExpression(Ast *expr)
  7028. {
  7029.     AstAssignmentExpression *assignment_expression = (AstAssignmentExpression *) expr;
  7030.  
  7031.     AstExpression *left_hand_side = assignment_expression -> left_hand_side;
  7032.  
  7033.     ProcessExpression(left_hand_side);
  7034.     ProcessExpressionOrStringConstant(assignment_expression -> expression);
  7035.     TypeSymbol *left_type = left_hand_side -> Type(),
  7036.                *right_type = assignment_expression -> expression -> Type();
  7037.  
  7038.     assignment_expression -> symbol = left_type;
  7039.  
  7040.     if (left_type == control.no_type || right_type == control.no_type)
  7041.         return;
  7042.  
  7043.     if (! left_hand_side -> ArrayAccessCast()) // the left-hand-side is a name
  7044.     {
  7045.         MethodSymbol *read_method = NULL;
  7046.         AstSimpleName *simple_name = left_hand_side -> SimpleNameCast();
  7047.         if (simple_name)
  7048.         {
  7049.             if (simple_name -> resolution_opt)
  7050.                read_method = simple_name -> resolution_opt -> symbol -> MethodCast();
  7051.         }
  7052.         else
  7053.         {
  7054.             AstFieldAccess *field_access = (AstFieldAccess *) left_hand_side;
  7055.             if (field_access -> resolution_opt)
  7056.                 read_method = field_access -> resolution_opt -> symbol -> MethodCast();
  7057.         }
  7058.  
  7059.         if (read_method)
  7060.         {
  7061.             VariableSymbol *symbol = (VariableSymbol *) read_method -> accessed_member;
  7062.             assignment_expression -> write_method = read_method -> containing_type -> GetWriteAccessMethod(symbol);
  7063.         }
  7064.     }
  7065.     else // the left-hand-side is an array access
  7066.     {
  7067.         //
  7068.         // TODO: Is this needed ?
  7069.         //
  7070.         // This operation may throw ArrayStoreException
  7071.         //
  7072.         SymbolSet *exception_set = TryExceptionTableStack().Top();
  7073.         if (exception_set && (! left_type -> Primitive()))
  7074.         {
  7075.             exception_set -> AddElement(control.RuntimeException());
  7076.         }
  7077.     }
  7078.  
  7079.     if (assignment_expression -> assignment_tag == AstAssignmentExpression::SIMPLE_EQUAL)
  7080.     {
  7081.         if (left_type != right_type)
  7082.         {
  7083.             if (CanAssignmentConvert(left_type, assignment_expression -> expression))
  7084.                 assignment_expression -> expression = ConvertToType(assignment_expression -> expression, left_type);
  7085.             else if (assignment_expression -> expression -> IsConstant() &&
  7086.                      control.IsSimpleIntegerValueType(left_type) &&
  7087.                      control.IsSimpleIntegerValueType(assignment_expression -> expression -> Type()))
  7088.             {
  7089.                 if (left_type == control.byte_type)
  7090.                      ReportSemError(SemanticError::INVALID_BYTE_VALUE,
  7091.                                     assignment_expression -> expression -> LeftToken(),
  7092.                                     assignment_expression -> expression -> RightToken());
  7093.                 else if (left_type == control.short_type)
  7094.                      ReportSemError(SemanticError::INVALID_SHORT_VALUE,
  7095.                                     assignment_expression -> expression -> LeftToken(),
  7096.                                     assignment_expression -> expression -> RightToken());
  7097.                 else if (left_type == control.int_type)
  7098.                      ReportSemError(SemanticError::INVALID_INT_VALUE,
  7099.                                     assignment_expression -> expression -> LeftToken(),
  7100.                                     assignment_expression -> expression -> RightToken());
  7101.                 else // assert(left_type == control.char_type);
  7102.                      ReportSemError(SemanticError::INVALID_CHARACTER_VALUE,
  7103.                                     assignment_expression -> expression -> LeftToken(),
  7104.                                     assignment_expression -> expression -> RightToken());
  7105.             }
  7106.             else
  7107.             {
  7108.                 ReportSemError(SemanticError::INCOMPATIBLE_TYPE_FOR_ASSIGNMENT,
  7109.                                assignment_expression -> LeftToken(),
  7110.                                assignment_expression -> RightToken(),
  7111.                                left_type -> ContainingPackage() -> PackageName(),
  7112.                                left_type -> ExternalName(),
  7113.                                right_type -> ContainingPackage() -> PackageName(),
  7114.                                right_type -> ExternalName());
  7115.             }
  7116.         }
  7117.  
  7118.         return;
  7119.     }
  7120.  
  7121.     //
  7122.     // In the current spec, it is stated that the type of both the left-hand
  7123.     // and right-hand side of an "op=" assignment must be primitive. However,
  7124.     // the left-hand side may be of type String if the operator is "+=" and in
  7125.     // that case, the right-hand side may also be of type String (or anything
  7126.     // else).
  7127.     //
  7128.     // TODO: CONFIRM THAT THERE WAS A MISTAKE IN THE SPEC.
  7129.     //
  7130.     if (left_type == control.String() && assignment_expression -> assignment_tag == AstAssignmentExpression::PLUS_EQUAL)
  7131.     {
  7132.         if (assignment_expression -> expression -> value == control.NullValue())
  7133.         {
  7134.             assignment_expression -> expression -> value = control.null_literal;
  7135.             assignment_expression -> expression -> symbol = control.String();
  7136.         }
  7137.         else if (right_type != control.String())
  7138.         {
  7139.             if (right_type == control.void_type)
  7140.                  ReportSemError(SemanticError::VOID_TO_STRING,
  7141.                                 assignment_expression -> expression -> LeftToken(),
  7142.                                 assignment_expression -> expression -> RightToken());
  7143.             else assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.String());
  7144.         }
  7145.  
  7146.         return;
  7147.     }
  7148.  
  7149.     if (! left_type -> Primitive())
  7150.     {
  7151.         ReportSemError(SemanticError::TYPE_NOT_PRIMITIVE,
  7152.                        left_hand_side -> LeftToken(),
  7153.                        left_hand_side -> RightToken(),
  7154.                        left_type -> Name());
  7155.         return;
  7156.     }
  7157.  
  7158.     if (! right_type -> Primitive())
  7159.     {
  7160.         ReportSemError(SemanticError::TYPE_NOT_PRIMITIVE,
  7161.                        assignment_expression -> expression -> LeftToken(),
  7162.                        assignment_expression -> expression -> RightToken(),
  7163.                        right_type -> Name());
  7164.         return;
  7165.     }
  7166.  
  7167.     switch(assignment_expression -> assignment_tag)
  7168.     {
  7169.         case AstAssignmentExpression::PLUS_EQUAL:
  7170.         case AstAssignmentExpression::STAR_EQUAL:
  7171.         case AstAssignmentExpression::MINUS_EQUAL:
  7172.             BinaryNumericPromotion(assignment_expression);
  7173.             break;
  7174.         case AstAssignmentExpression::SLASH_EQUAL:
  7175.         case AstAssignmentExpression::MOD_EQUAL:
  7176.             BinaryNumericPromotion(assignment_expression);
  7177.             {
  7178.                 AstExpression *right_expression = assignment_expression -> expression;
  7179.                 if (right_expression -> IsConstant())
  7180.                 {
  7181.                     //
  7182.                     // If the type of the expression is int or long and the right-hand side is 0
  7183.                     // then issue an error message.
  7184.                     //
  7185.                     if ((left_type == control.int_type && ((IntLiteralValue *) right_expression -> value) -> value == 0) ||
  7186.                         (left_type == control.long_type && ((LongLiteralValue *) right_expression -> value) -> value == 0))
  7187.                     {
  7188.                         ReportSemError(SemanticError::ZERO_DIVIDE_CAUTION,
  7189.                                        assignment_expression -> LeftToken(),
  7190.                                        assignment_expression -> RightToken());
  7191.                     }
  7192.                 }
  7193.             }
  7194.             break;
  7195.         case AstAssignmentExpression::LEFT_SHIFT_EQUAL:
  7196.         case AstAssignmentExpression::RIGHT_SHIFT_EQUAL:
  7197.         case AstAssignmentExpression::UNSIGNED_RIGHT_SHIFT_EQUAL:
  7198.              if (! control.IsIntegral(left_type))
  7199.              {
  7200.                  ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  7201.                                 left_hand_side -> LeftToken(),
  7202.                                 left_hand_side -> RightToken(),
  7203.                                 left_type -> Name());
  7204.              }
  7205.  
  7206.              if (! control.IsIntegral(right_type))
  7207.              {
  7208.                  ReportSemError(SemanticError::TYPE_NOT_INTEGRAL,
  7209.                                 assignment_expression -> expression -> LeftToken(),
  7210.                                 assignment_expression -> expression -> RightToken(),
  7211.                                 right_type -> Name());
  7212.              }
  7213.  
  7214.              assignment_expression -> left_hand_side = PromoteUnaryNumericExpression(left_hand_side);
  7215.              assignment_expression -> expression = PromoteUnaryNumericExpression(assignment_expression -> expression);
  7216.              if (assignment_expression -> expression -> Type() == control.long_type)
  7217.                  assignment_expression -> expression = ConvertToType(assignment_expression -> expression, control.int_type);
  7218.              break;
  7219.         case AstAssignmentExpression::AND_EQUAL:
  7220.         case AstAssignmentExpression::XOR_EQUAL:
  7221.         case AstAssignmentExpression::IOR_EQUAL:
  7222.              if (left_type != control.boolean_type || right_type != control.boolean_type) // if anyont of the exprs is not boolean
  7223.                  BinaryNumericPromotion(assignment_expression);
  7224.              break;
  7225.         default:
  7226.             assert(false);
  7227.             break;
  7228.     }
  7229.  
  7230.     return;
  7231. }
  7232.